Questions tagged [dcast]
267 questions
3
votes
1 answer
dcast for numeric and character columns in R - returning length by default
I have a data which looks like this :-
data_source zip date calories user price
compA 45768 18274 3500 abc 912.27
compB 33098 18274 3500 groups …

relu
- 333
- 1
- 3
- 18
3
votes
0 answers
How to Use data.table dcast Renaming on one Aggregation Function
dcast.data.table automatically renames columns when multiple aggregation functions are used. How can I use this rename when I use only one function?
DT = melt(as.data.table(ChickWeight), id=2:4)
% automatic rename
data.table::dcast(DT, Time ~…

Dirk
- 1,134
- 2
- 12
- 21
3
votes
2 answers
Casting data in R with more than one variable
I would like to cast data from long to wide but show variables as rows and not columns
Dataset:
df1 <- data.frame(Area = c('a', 'a', 'b', 'b', 'c', 'c'),
Period =c(1,2,1,2,1,2),
var1 = c(1,2,3,4,5,6),
…

Neil
- 33
- 3
3
votes
2 answers
R data.table - dcast multiple groups for single ID into multiple columns
When dcasting a data.table, if an id falls under multiple categories / fields, it returns the length of values by default.
dta <- (data.table(ID = c("A", "C", "B", "A", "D", "D", "A", "B", "D", "D"),
CATEGORY_DUPLICATE =…

Sanjid
- 67
- 6
3
votes
3 answers
data.table: How to change column values based on grouped unique row values that contain column names
I have a data.table with ~18^6 rows and I need to take the unique values of CLASS, by ID, and set their respective columns to 1, as seen below in a baby example
DT <- data.table::data.table(ID=c("1","1","1","2","2"),
…

theneil
- 488
- 1
- 4
- 14
3
votes
1 answer
r data.table::dcast cross product fails on large data set
i have a dcast() application whose cross-product exceeds .Machine$integer.max. is there a recommended alternative to dealing with this situation? i could break up w into smaller pieces, but was hoping for a clean solution.
this might be a…

Anthony Damico
- 5,779
- 7
- 46
- 77
3
votes
1 answer
dcast not retaining variable type as character, error in vapply when a variable is NA
In attempting to reshape data using resphape2::dcast, I am encountering an error involving NA entries. Sample data are at the end.
The data are reshaped from long to wide, but on occasion some parameters have all NA entries, which appears to be…

Anonymous coward
- 2,061
- 1
- 16
- 29
3
votes
6 answers
Nth smallest value for every column in data.frame in R
I want to find the nth smallest number for every column in a data.frame.
In the below example I specify actually the second smallest value using the dcast nth function. Can someone help with the coding of the…

Tzac
- 101
- 2
- 12
3
votes
0 answers
data.table dcast with 1 variable
Example
require(data.table)
d <- data.table(a=sample(1:10,100,replace=T))
dcast(d[,.N,a],~a,value.var="N")
The above dcast returns the following message
Error in check_formula(formula, names(data), valnames) :
Invalid formula. Cast formula…

ironv
- 978
- 10
- 25
3
votes
1 answer
R using dcast,melt and concatenation to reshape data frame
I have a data frame as follows:
mydf <- data.frame(Term = c('dog','cat','lion','tiger','pigeon','vulture'), Category = c('pet','pet','wild','wild','pet','wild'),
Count = c(12,14,19,7,11,10), Rate = c(0.4,0.7,0.3,0.6,0.1,0.8), Brand =…

Tarak
- 1,035
- 2
- 8
- 14
3
votes
2 answers
Combine several dcast data.table (which share same key) efficiently
Here is the simple problem I'm trying to solve: I have a data.table like following table, and I'm trying to use dcast.data.table function to calculate number of advancement for each group, but also I'm interested to calculate median of grades in…

Mahdi Jadaliha
- 1,947
- 1
- 14
- 22
2
votes
3 answers
Convert the event-level dataset to the patient-level data in r
I need to convert the event-level dataset to the patient-level data i.e. convert a long dataset to a wider one based on deidnum as a key variable. Additionally, I want to create columns for each produced event and its event time. In case the same…

Mohamed Rahouma
- 1,084
- 9
- 20
2
votes
2 answers
Find and return column with max value from a list of vectors
I'm trying to calculate favorite brand per productcategory per customer.
1. Start and what I want to achieve
I start with a dataframe (mydata) with purchase orders, build up like…

Frank
- 21
- 2
2
votes
2 answers
reshape from wide to long, simple data.table issue
I know there must be a one-line data.table solution for this, probably with dcast, but I can't figure it out.
I have data like this:
library(data.table)
data1 <- data.table(
id = seq(1:5),
code = c("A","A","B","A","B"),
date = as.Date(…

epiNS
- 353
- 2
- 7
2
votes
1 answer
R data.table - How to modify by reference when using .SD?
So I'm new to data.table and don't understand now I can modify by reference at the same time that I perform an operation on chosen columns using the .SD symbol? I have two examples.
Example 1
> DT <- data.table("group1:1" = 1, "group1:2" = 1,…

Jeff Groh
- 135
- 11