reshape2

dcast with custom fun.aggregate

牧云@^-^@ 提交于 2019-12-10 10:45:43
问题 I have data that looks like this: sample start end gene coverage X 1 10 A 5 X 11 20 A 10 Y 1 10 A 5 Y 11 20 A 10 X 1 10 B 5 X 11 20 B 10 Y 1 10 B 5 Y 11 20 B 10 I added additional columns: data$length <- (data$end - data$start + 1) data$ct_lt <- (data$length * data$coverage) I reformated my data using dcast: casted <- dcast(data, gene ~ sample, value.var = "coverage", fun.aggregate = mean) So my new data looks like this: gene X Y A 10.00000 10.00000 B 38.33333 38.33333 This is the correct

reshape a dataframe with tidyr or reshape2 [duplicate]

不想你离开。 提交于 2019-12-08 11:21:57
问题 This question already has answers here : Reshaping multiple sets of measurement columns (wide format) into single columns (long format) (7 answers) Closed 3 years ago . I would like to transforme this dataset : ID v1 v2 v3 c1 c2 c3 1 1 -3 -11 -2 -6 -1 -1 2 2 -10 -4 -12 -11 4 6 3 3 4 -4 15 5 1 -3 4 4 -6 0 -6 5 -1 8 5 5 -7 12 6 -12 -11 11 input<-structure(list(ID = 1:5, v1 = c(-3, -10, 4, -6, -7), v2 = c(-11, -4, -4, 0, 12), v3 = c(-2, -12, 15, -6, 6), c1 = c(-6, -11, 5, 5, -12), c2 = c(-1, 4,

Make rows with multiple observations into columns

 ̄綄美尐妖づ 提交于 2019-12-08 04:50:15
问题 I was looking at similar questions but I couldn't find a case similar to mine. I have a data frame that for each subject, has multiple observations per condition. It looks like this: subject <- c(rep("S1",4), rep("S2",4)) condition <- rep(c(rep("a",2), rep("b",2)),2) value <- c(1:8) df <- data.frame(subject,condition,value) df subject condition value S1 a 1 S1 a 2 S1 b 3 S1 b 4 S2 a 5 S2 a 6 S2 b 7 S2 b 8 I would like to reshape it to look like this: subject condition.a condition.b S1 1 3 S1

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

Seasonal aggregate of monthly data

不问归期 提交于 2019-12-08 00:25:55
问题 I have dataframe df with x,y,and monthly.year data for each x,y point. I am trying to get the seasonal aggregate. I need to calculate seasonal means i.e. For winter mean of (December,January,February); for Spring mean of (March,April,May), for Summer mean of (June,July,August) and for autumn mean of (September,October,November). The data looks similar to: set.seed(1) df <- data.frame(x=1:3,y=1:3, matrix(rnorm(72),nrow=3) ) names(df)[3:26] <- paste(month.abb,rep(2009:2010,each=12),sep=".") x y

Convert long data frame into wide data frame

泄露秘密 提交于 2019-12-07 13:24:42
问题 I am trying to convert long data form into wide data form in R. For instance, I have following data frame: a = rep(c("A","B","C","D"),4) b = rep(c("COL1","COL2","COL3","COL4"),4) val = 101:116 df = as.data.frame(cbind(a,b,val)) df I would like to see the result as: row <- as.list(levels(df$a)) col <- as.list(levels(df$b)) test <- data.frame() i = 1 for (j in 1:4) { for(k in 1:4){ test[j,k] = df$val[i] i = i + 1 } } colnames(test) <- c("COL1","COL2","COL3","COL4") rownames(test) <- c("A","B",

POSIXct values become numeric in reshape2 dcast

*爱你&永不变心* 提交于 2019-12-07 03:22:35
问题 I'm trying to use dcast from the latest reshape2 package (1.2.1) to denormalize a data frame (or data.table) where the value.var is a POSIXct type, but in the resulting data frame, the date values have lost their POSIXct class and become numeric. Do I really have to as.POSIXct() every generated column if I want the values back as POSIXct's, or am I missing something? x <- c("a","b"); y <- c("c","d"); z <- as.POSIXct(c("2012-01-01 01:01:01","2012-02-02 02:02:02")); d <- data.frame(x, y, z,

Make rows with multiple observations into columns

只愿长相守 提交于 2019-12-06 16:12:01
I was looking at similar questions but I couldn't find a case similar to mine. I have a data frame that for each subject, has multiple observations per condition. It looks like this: subject <- c(rep("S1",4), rep("S2",4)) condition <- rep(c(rep("a",2), rep("b",2)),2) value <- c(1:8) df <- data.frame(subject,condition,value) df subject condition value S1 a 1 S1 a 2 S1 b 3 S1 b 4 S2 a 5 S2 a 6 S2 b 7 S2 b 8 I would like to reshape it to look like this: subject condition.a condition.b S1 1 3 S1 2 4 S2 5 7 S2 6 8 I have tried reshape and cast , but they give me an error message because there are

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 with custom fun.aggregate

做~自己de王妃 提交于 2019-12-06 12:05:49
I have data that looks like this: sample start end gene coverage X 1 10 A 5 X 11 20 A 10 Y 1 10 A 5 Y 11 20 A 10 X 1 10 B 5 X 11 20 B 10 Y 1 10 B 5 Y 11 20 B 10 I added additional columns: data$length <- (data$end - data$start + 1) data$ct_lt <- (data$length * data$coverage) I reformated my data using dcast: casted <- dcast(data, gene ~ sample, value.var = "coverage", fun.aggregate = mean) So my new data looks like this: gene X Y A 10.00000 10.00000 B 38.33333 38.33333 This is the correct data format I desire, but I would like to fun.aggregate differently. Instead, I would like to take a