dcast

Casting multiple value.var controled by fun.aggregate

假装没事ソ 提交于 2019-12-11 05:52:55
问题 I have the following dataset client_id <- c("A", "A", "B", "B", "B", "B", "B", "A", "A", "B", "B") value <- c(10, 35, 20, 30, 50, 40, 30, 40, 30, 40, 10) period_30 <- c(1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0) period_60 <- c(1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0) sign <- c("D", "D", "D", "D", "C", "C", "C", "D", "D", "D", "D") data <- data.frame(client_id, value, period_30, period_60, sign) I can use this code to count the number of different splits per given period with the code below: library(data.table)

R - Pivot table with subtotals

给你一囗甜甜゛ 提交于 2019-12-08 03:36:55
问题 How do I get Pivots with subtotals (like in MS Excel Pivot Tables) in R? I am using dcast from reshape2 package to create pivots in R. I also got grand totals working using rowSums and colSums . I admit I do not understand the intricacies in the dcast parameter set. I just know how to create the pivot and the help file is going over my head. It will be extremely helpful if someone can solve this using dcast (I suspect it can do it all), and explain the parameters necessary for the solution. I

dcast fails to cast character column when the data size is large

我怕爱的太早我们不能终老 提交于 2019-12-08 01:10:27
问题 I'm using the dcast function in the library(reshape2) package to cast a simple table of three columns df = data.table(id = 1:1e6, var = c('continent','subcontinent',...), val = c('America','Caribbean',...)```` by dcast(df, id ~ var, value.var ='val') and it automatically converts the value to the count, i.e. id continent subcontinent 1 1 1 2 1 1 However, if I reduce the size to 10000 rows, it correctly outputs id continent subcontinent 1 America Caribbean 2 Europe West Europe Is this a bug or

R - Pivot table with subtotals

﹥>﹥吖頭↗ 提交于 2019-12-06 13:29:19
How do I get Pivots with subtotals (like in MS Excel Pivot Tables) in R? I am using dcast from reshape2 package to create pivots in R. I also got grand totals working using rowSums and colSums . I admit I do not understand the intricacies in the dcast parameter set. I just know how to create the pivot and the help file is going over my head. It will be extremely helpful if someone can solve this using dcast (I suspect it can do it all), and explain the parameters necessary for the solution. I am using this code (C2 has two factors, X1 & X2): PIV <- dcast(DF, C1~C2, value.var="C3", sum) I am

dcast fails to cast character column when the data size is large

假如想象 提交于 2019-12-06 10:31:43
I'm using the dcast function in the library(reshape2) package to cast a simple table of three columns df = data.table(id = 1:1e6, var = c('continent','subcontinent',...), val = c('America','Caribbean',...)```` by dcast(df, id ~ var, value.var ='val') and it automatically converts the value to the count, i.e. id continent subcontinent 1 1 1 2 1 1 However, if I reduce the size to 10000 rows, it correctly outputs id continent subcontinent 1 America Caribbean 2 Europe West Europe Is this a bug or I need to change the code somehow? Please help. Thanks! The problem is not the size of the dataset

R reshape2 dcast: transform data

允我心安 提交于 2019-12-02 19:13:09
问题 How can I transform data X to Y as in X = data.frame( ID = c(1,1,1,2,2), NAME = c("MIKE","MIKE","MIKE","LUCY","LUCY"), SEX = c("MALE","MALE","MALE","FEMALE","FEMALE"), TEST = c(1,2,3,1,2), SCORE = c(70,80,90,65,75) ) Y = data.frame( ID = c(1,2), NAME = c("MIKE","LUCY"), SEX = c("MALE","FEMALE"), TEST_1 =c(70,65), TEST_2 =c(80,75), TEST_3 =c(90,NA) ) The dcast function in reshape2 seems to work but it can not include other columns in the data like ID, NAME and SEX in the example above.

R reshape2 dcast: transform data

放肆的年华 提交于 2019-12-02 08:30:46
How can I transform data X to Y as in X = data.frame( ID = c(1,1,1,2,2), NAME = c("MIKE","MIKE","MIKE","LUCY","LUCY"), SEX = c("MALE","MALE","MALE","FEMALE","FEMALE"), TEST = c(1,2,3,1,2), SCORE = c(70,80,90,65,75) ) Y = data.frame( ID = c(1,2), NAME = c("MIKE","LUCY"), SEX = c("MALE","FEMALE"), TEST_1 =c(70,65), TEST_2 =c(80,75), TEST_3 =c(90,NA) ) The dcast function in reshape2 seems to work but it can not include other columns in the data like ID, NAME and SEX in the example above. Assuming all other columns by a ID column are consistent, like Mike can only be a male with ID 1, how can we

R: Melt and Dcast

北城余情 提交于 2019-12-02 04:03:36
I have a dataset like this: CASE_ID = c("C1","C1", "C2","C2", "C2", "C3", "C4") PERSON_ID = c(1,0,7,8,1,20,7) PERSON_DIVISION = c("Zone 1", "NA", "Zone 1", "Zone 3", "Zone 1", "Zone 5", "Zone 1") df <- data.frame(CASE_ID, PERSON_ID, PERSON_DIVISION) df That results in: CASE_ID PERSON_ID PERSON_DIVISION 1 C1 1 Zone 1 2 C1 0 NA 3 C2 7 Zone 1 4 C2 8 Zone 3 5 C2 1 Zone 1 6 C3 20 Zone 5 7 C4 7 Zone 1 And I want to transform it in: CASE_ID P1_ID P2_ID P3_ID P1_Division P2_Division P3_Division 1 1 0 NA Zone 1 NA NA 2 7 8 1 Zone 1 Zone 3 Zone 1 3 20 NA NA Zone 5 NA NA 4 7 NA NA Zone 1 NA NA My

Reshape data from long to wide format - more than one variable [duplicate]

谁说胖子不能爱 提交于 2019-11-29 13:02:58
This question already has an answer here: Reshape multiple values at once 2 answers I’m trying to reshape my data from long to wide formula using the dcast function from reshape2 . The objective is to use different variables in the value.var parameter but R doesn't let me use more than one value in it. Is there any other way I could fix it? I've looked at other similar questions but I haven't been able to find a similar examples. Here is my current dataset: +---------+------+--------+--------------+------------+ | Country | Year | Growth | Unemployment | Population | +---------+------+--------

How to split one column into different columns with dcast without aggregating?

喜夏-厌秋 提交于 2019-11-28 06:58:54
问题 I'm trying to reshape my data using dcast. I'm working with samples where each sample has 10-30 sample units. I can't have my data aggregate. My data is in this format: ID total sample_1 1 sample_1 0 sample_1 2 sample_1 1 sample_1 0 sample_1 0 sample_1 2 sample_1 1 sample_1 0 sample_1 2 sample_1 1 sample_1 4 sample_2 2 sample_2 1 sample_2 2 sample_2 0 sample_2 0 sample_2 0 sample_2 1 sample_2 2 sample_2 1 sample_2 4 sample_2 5 sample_2 2 sample_2 1 sample_3 0 sample_3 0 sample_3 1 sample_3 2