reshape2

Reshape wide format, to multi-column long format

淺唱寂寞╮ 提交于 2019-11-28 21:28:42
I want to reshape a wide format dataset that has multiple tests which are measured at 3 time points: ID Test Year Fall Spring Winter 1 1 2008 15 16 19 1 1 2009 12 13 27 1 2 2008 22 22 24 1 2 2009 10 14 20 2 1 2008 12 13 25 2 1 2009 16 14 21 2 2 2008 13 11 29 2 2 2009 23 20 26 3 1 2008 11 12 22 3 1 2009 13 11 27 3 2 2008 17 12 23 3 2 2009 14 9 31 into a data set that separates the tests by column but converts the measurement time into long format, for each of the new columns like this: ID Year Time Test1 Test2 1 2008 Fall 15 22 1 2008 Spring 16 22 1 2008 Winter 19 24 1 2009 Fall 12 10 1 2009

Reshaping a data frame with more than one measure variable

我的梦境 提交于 2019-11-28 21:18:54
I'm using a data frame similar to this one: df<-data.frame(student=c(rep(1,5),rep(2,5)), month=c(1:5,1:5), quiz1p1=seq(20,20.9,0.1),quiz1p2=seq(30,30.9,0.1), quiz2p1=seq(80,80.9,0.1),quiz2p2=seq(90,90.9,0.1)) print(df) student month quiz1p1 quiz1p2 quiz2p1 quiz2p2 1 1 1 20.0 30.0 80.0 90.0 2 1 2 20.1 30.1 80.1 90.1 3 1 3 20.2 30.2 80.2 90.2 4 1 4 20.3 30.3 80.3 90.3 5 1 5 20.4 30.4 80.4 90.4 6 2 1 20.5 30.5 80.5 90.5 7 2 2 20.6 30.6 80.6 90.6 8 2 3 20.7 30.7 80.7 90.7 9 2 4 20.8 30.8 80.8 90.8 10 2 5 20.9 30.9 80.9 90.9 Describing grades received by students during five months – in two quizzes

quick/elegant way to construct mean/variance summary table

隐身守侯 提交于 2019-11-28 18:46:17
I can achieve this task, but I feel like there must be a "best" (slickest, most compact, clearest-code, fastest?) way of doing it and have not figured it out so far ... For a specified set of categorical factors I want to construct a table of means and variances by group. generate data : set.seed(1001) d <- expand.grid(f1=LETTERS[1:3],f2=letters[1:3], f3=factor(as.character(as.roman(1:3))),rep=1:4) d$y <- runif(nrow(d)) d$z <- rnorm(nrow(d)) desired output : f1 f2 f3 y.mean y.var 1 A a I 0.6502307 0.09537958 2 A a II 0.4876630 0.11079670 3 A a III 0.3102926 0.20280568 4 A b I 0.3914084 0

reshape2 melt warning message

你说的曾经没有我的故事 提交于 2019-11-28 16:58:33
问题 I'm using melt and encounter the following warning message: attributes are not identical across measure variables; they will be dropped After looking around people have mentioned it is because the variables are different classes; however, that is not the case with my dataset. Here is the dataset: test <- structure(list(park = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = c("miss", "piro", "sacn", "slbe"), class = "factor"), a1.one = structure(c(3L, 1L, 3L, 3L, 3L, 3L, 1L, 3L,

How do I subset column variables in DF1 based on the important variables I got in DF2?

泄露秘密 提交于 2019-11-28 14:31:34
I have 2 df's like this ID = c('x1','x2','x5') df1 <- data.frame(ID) x1 = c(1,2,3,4,5) x2 = c(11,12,13,14,15) x3 = c(21,22,23,24,25) x4 = c(31,32,33,34,35) x5 = c(41,42,43,44,45) df2 <- data.frame(x1,x2,x3,x4,x5) Desired output x1 x2 x5 1 1 11 41 2 2 12 42 3 3 13 43 4 4 14 44 5 5 15 45 I would like my new dataset to contain only those variables that are identified in df1 as important (i.e: x1,x2,x5) with the values from df2. In this simple dataset, I know I could do this but just removing x3,x4 in df2 but ideally I would like to apply it to a larger data set where I have more than 100

How to use a character vector of column names in the formula argument of dcast (reshape2)

牧云@^-^@ 提交于 2019-11-28 12:49:17
Say I have a dataframe df with dozens of identifying variables (in columns) and only a few measured variables (also in columns). To avoid repetitively typing all the variables for each argument, I assign the names of the identifying and measured df columns to df_id and df_measured , respectively. It's easy enough to input these vectors to shorten the argument inputs for melt ... df.m <- melt(df, id.vars = df_id, measure.vars = df_measured) ... but I'm at a loss for how to enter the formula = argument in dcast using the same method to specify my id variables since it requires that the input

From long to wide data with multiple columns

做~自己de王妃 提交于 2019-11-28 12:19:41
Suggestions for how to smoothly get from foo to foo2 (preferably with tidyr or reshape2 packages)? This is kind of like this question , but not exactly I think, because I don't want to auto-number columns, just widen multiple columns. It's also kind of like this question , but again, I don't think I want the columns to vary with a row value as in that answer. Or, a valid answer to this question is to convince me it's exactly like one of the others. The solution in the second question of "two dcasts plus a merge" is the most attractive right now, because it is comprehensible to me. foo: foo =

reshape multi id repeated variable readings from long to wide

ⅰ亾dé卋堺 提交于 2019-11-28 11:46:21
This is what I have: id<-c(1,1,1,1,1,1,1,1,1,2,2,2,2,2,2,2,2,2) measure<-c("speed","weight","time","speed","weight","time","speed","weight","time", "speed","weight","time","speed","weight","time","speed","weight","time") value<-c(1.23,10.3,33,1.44,10.4,31,1.21,10.1,33,4.25,12.5,38,1.74,10.8,31,3.21,10.3,33) testdf<-data.frame(id,measure,value) This is what I want: id<-c(1,1,1,2,2,2) speed<-c(1.23,1.44,1.21,4.25,1.74,3.21) weight<-c(10.3,10.4,10.1,12.5,10.8,10.3) time<-c(33,31,33,37,31,33) res<-data.frame(id,speed,weight,time) The issue lies in that my variables speed weight and time are

In R: get multiple rows by splitting a column using tidyr and reshape2 [duplicate]

谁都会走 提交于 2019-11-28 10:16:44
问题 This question already has an answer here: Split comma-separated strings in a column into separate rows 5 answers What is the most simpel way using tidyr or reshape2 to turn this data: data <- data.frame( A=c(1,2,3), B=c("b,g","g","b,g,q")) Into (e.g. make a row for each comma separated value in variable B ): A B 1 1 b 2 1 g 3 2 g 4 3 b 5 3 g 6 3 q 回答1: Try library(splitstackshape) cSplit(data, 'B', ',', 'long') Or using base R lst <- setNames(strsplit(as.character(data$B), ','), data$A) stack

Changing Values from Wide to Long: 1) Group_By, 2) Spread/Dcast [duplicate]

ぃ、小莉子 提交于 2019-11-28 09:15:09
问题 This question already has answers here : Transpose / reshape dataframe without “timevar” from long to wide format (6 answers) Closed 5 months ago . I've got a list of names of phone numbers, which I want to group by name, and bring them from a long format to a wide one, with the phone number filling across the columns Name Phone_Number John Doe 0123456 John Doe 0123457 John Doe 0123458 Jim Doe 0123459 Jim Doe 0123450 Jane Doe 0123451 Jill Doe 0123457 Name Phone_Number1 Phone_Number2 Phone