reshape2

Reshape wide format, to multi-column long format

只谈情不闲聊 提交于 2019-11-27 13:51:10
问题 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

Reshaping a data frame with more than one measure variable

拈花ヽ惹草 提交于 2019-11-27 13:41:59
问题 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

quick/elegant way to construct mean/variance summary table

我怕爱的太早我们不能终老 提交于 2019-11-27 11:49:13
问题 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

Reshape a dataframe to long format with multiple sets of measure columns [duplicate]

允我心安 提交于 2019-11-27 09:47:11
This question already has an answer here: Reshaping multiple sets of measurement columns (wide format) into single columns (long format) 7 answers I have an R dataframe that I scraped from the internet using readHTMLTable() in the XML package. The table looks like the following excerpt with multiple variables/columns for population and year. (Note that the years are not duplicated across columns and represent a unique identifier for population.) year1 pop1 year2 pop2 year3 pop3 1 2 16XX 4675,0 1900 6453,0 1930 9981,2 3 17XX 4739,3 1901 6553,5 1931 ... 4 17XX 4834,0 1902 6684,0 1932 5 180X 4930

Comparing gather (tidyr) to melt (reshape2)

廉价感情. 提交于 2019-11-27 09:27:58
问题 I love the reshape2 package because it made life so doggone easy. Typically Hadley has made improvements in his previous packages that enable streamlined, faster running code. I figured I'd give tidyr a whirl and from what I read I thought gather was very similar to melt from reshape2 . But after reading the documentation I can't get gather to do the same task that melt does. Data View Here's a view of the data (actual data in dput form at end of post): teacher yr1.baseline pd yr1.lesson1 yr1

Reshaping data frame with duplicates

耗尽温柔 提交于 2019-11-27 09:07:25
I have what should be a simple reshaping problem, but I can't figure it out. Part of my data looks like this: foo <- structure(list(grade = c(3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 3, 3, 4, 4, 5, 5, 6, 6), var.type = structure(c(3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L ), .Label = c("Raw Score", "SE", "SS"), class = "factor"), var.val = c(120L, 47L, 120L, 46L, 120L, 46L, 120L, 47L, 120L, 46L, 120L, 46L, 120L, 12L, 120L, 14L, 120L, 16L, 120L, 20L)), .Names = c("grade", "var.type", "var.val"), row.names = c(2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L,

how to spread or cast multiple values in r [duplicate]

纵然是瞬间 提交于 2019-11-27 09:06:16
This question already has an answer here: can the value.var in dcast be a list or have multiple value variables? 3 answers Convert data from long format to wide format with multiple measure columns 4 answers Here is toy data set for this example: data <- data.frame(x=rep(c("red","blue","green"),each=4), y=rep(letters[1:4],3), value.1 = 1:12, value.2 = 13:24) x y value.1 value.2 1 red a 1 13 2 red b 2 14 3 red c 3 15 4 red d 4 16 5 blue a 5 17 6 blue b 6 18 7 blue c 7 19 8 blue d 8 20 9 green a 9 21 10 green b 10 22 11 green c 11 23 12 green d 12 24 How can I cast or spread variable y, to

can the value.var in dcast be a list or have multiple value variables?

隐身守侯 提交于 2019-11-27 07:48:10
In the help files for dcast.data.table , there is a note stating that a new feature has been implemented: "dcast.data.table allows value.var column to be of type list" I take this to mean that one can have multiple value variables within a list, i.e. in this format: dcast.data.table(dt, x1~x2, value.var=list('var1','var2','var3')) But we get an error: 'value.var' must be a character vector of length 1. Is there such a feature, and if not, what would be other one-liner alternatives? EDIT: In reply to the comments below There are situations where you have multiple variables that you want to

Compute mean and standard deviation by group for multiple variables in a data.frame

自古美人都是妖i 提交于 2019-11-27 06:54:53
Edit -- This question was originally titled << Long to wide data reshaping in R >> I'm just learning R and trying to find ways to apply it to help out others in my life. As a test case, I'm working on reshaping some data, and I'm having trouble following the examples I've found online. What I'm starting with looks like this: ID Obs 1 Obs 2 Obs 3 1 43 48 37 1 27 29 22 1 36 32 40 2 33 38 36 2 29 32 27 2 32 31 35 2 25 28 24 3 45 47 42 3 38 40 36 And what I want to end up with will look like this: ID Obs 1 mean Obs 1 std dev Obs 2 mean Obs 2 std dev 1 x x x x 2 x x x x 3 x x x x And so forth. What

reshape multi id repeated variable readings from long to wide

时光总嘲笑我的痴心妄想 提交于 2019-11-27 06:28:35
问题 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