reshape2

Reshaping multiple groups of columns in a data frame from wide to long

无人久伴 提交于 2019-12-11 06:59:36
问题 I am working with air-quality data. I tried to reshape the data frame from wide to long using melt function. Here is the data: Elev stands for Elevation , Obs for observation and US3, DK1, DE1 are models, where lm and ul represents first and third quantiles. Elev Obs lm ul US3 lm ul DK1 lm ul 1 0 37.74289 34.33422 41.27840 38.82037 35.35241 42.30042 49.31111 45.00134 53.90968 2 100 38.14076 34.71842 41.36560 39.82727 36.49086 43.22209 50.46545 45.79068 55.44664 3 250 39.31056 35.98180 42

Transposing a data frame [duplicate]

不想你离开。 提交于 2019-12-11 06:42:43
问题 This question already has answers here : Reshape wide format, to multi-column long format (4 answers) Closed 6 years ago . I have a question about re-shaping (if that's the right word) a data frame to a transposed version of it. I want to take something like: A B C 1 6 1 1 18 1 1 21 1 3 18 1 3 21 1 4 6 1 4 18 1 4 20 1 4 21 1 And turn it into a dataframe like: A B_1 C_1 B_2 C_2 B_3 C_3 ... 1 6 1 18 1 21 1 3 18 1 21 1 4 6 1 18 1 20 1 21 1 Is there some go-to function in R that I'm unaware of or

Aggregating factor level counts - by factor

限于喜欢 提交于 2019-12-11 06:15:02
问题 I have been trying to make a table displaying the counts of factor levels by another factor. For this, I looked on dozens of pages, questions... trying to use functions in some packages (dplyr, reshape) to get the job done, without any success in using them correctly. That's what I got: # my data: var1 <- c("red","blue","red","blue","red","red","red","red","red","red","red","red","blue","red","blue") var2 <- c("0","1","0","0","0","0","0","0","0","0","1","0","0","0","0") var3 <- c("2","2","1",

Transform data frame into matrix with counts

跟風遠走 提交于 2019-12-11 03:39:15
问题 I have data files structured like this: OTU1 PIA0 1120 OTU2 PIA1 2 OTU2 PIA3 6 OTU2 PIA4 10 OTU2 PIA5 1078 OTU2 PIN1 24 OTU2 PIN2 45 OTU2 PIN3 261 OTU2 PIN4 102 OTU3 PIA0 16 OTU3 PIA1 59 OTU3 PIA2 27 OTU3 PIA3 180 OTU3 PIA4 200 OTU3 PIA5 251 OTU3 PIN0 36 OTU3 PIN1 61 OTU3 PIN2 156 OTU3 PIN3 590 OTU3 PIN4 277 OTU4 PIA0 401 OTU4 PIN0 2 And I want to create a matrix that shows combination of data from the second column taking the first column as reference for the counts of combination (showing

melt.data.table may crash when measure.vars contains variables not in data.table

孤街浪徒 提交于 2019-12-11 02:46:54
问题 The following code crashes in my installation: library(data.table) library(reshape2) x = data.table(a=c(1,2),b=c(2,3),c=c(3,4)) melt(x,id.vars="a") # OK melt(x,id.vars="a",measure.vars=c("c","d")) # Crashes The message is the following: *** caught segfault *** address 0x21000038, cause 'memory not mapped' Traceback: 1: melt.data.table(x, id.vars = "a", measure.vars = c("c", "d")) 2: melt(x, id.vars = "a", measure.vars = c("c", "d")) Here is the sessionInfo() R version 3.1.0 (2014-04-10)

add rows in a data.table but not when certain columns take same values

早过忘川 提交于 2019-12-10 23:58:03
问题 I have a data.table dat with 4 columns, say ( col1 , col2 , col3 , col4 ). Input data: structure(list(col1 = c(5.1, 5.1, 4.7, 4.6, 5, 5.1, 5.1, 4.7, 4.6, 5), col2 = c(3.5, 3.5, 3.2, 3.1, 3.6, 3.5, 3.5, 3.2, 3.1, 3.6), col3 = c(1.4, 1.4, 1.3, 1.5, 1.4, 3.4, 3.4, 1.3, 1.5, 1.4 ), col4 = structure(c(1L, 1L, 1L, 1L, 1L, 4L, 4L, 4L, 4L, 4L), .Label = c("setosa", "versicolor", "virginica", "eer"), class = "factor")), .Names = c("col1", "col2", "col3", "col4"), row.names = c(NA, -10L), class = c(

dplyr with subgroup join

大兔子大兔子 提交于 2019-12-10 21:45:14
问题 The following problem can be seen as a "two-column reshape to wide", and there are several methods available to solve it the classical way, from base::reshape (horror) to reshape2 . For the two-group case, a simple subgroup join works best. Can I reformulate the join within the piping framework of dplyr ? The example below is a bit silly, but I needed the join in a longer pipe-chain which I do not want to break. library(dplyr) d = data.frame(subject= rep(1:5,each=2),treatment=letters[1:2],bp

dcast renaming all variables to start with a number

为君一笑 提交于 2019-12-10 19:42:57
问题 So I've got data that looks like this: id year principal interest 1: 011000600 2013 0.00 0.00 2: 011000600 2014 544.03 0.00 3: 011000700 2013 0.00 0.00 4: 011000700 2014 0.01 0.00 5: 011000800 2013 363.44 12.79 6: 011000800 2014 2005.98 0.00 7: 011000900 2013 0.00 0.00 8: 011000900 2014 0.00 0.00 9: 011001000 2013 0.00 0.00 10: 011001000 2014 0.00 0.00 11: 011001100 2013 0.00 0.00 12: 011001100 2014 1723.24 0.00 13: 011001560 2013 0.00 0.00 14: 011001560 2014 0.00 0.00 15: 011001650 2013 0.00

R: include factors with no entries when using dcast

六眼飞鱼酱① 提交于 2019-12-10 19:27:54
问题 I am using the reshape2 function dcast on a dataframe. One of the variables is a factor where some of the levels do not appear in the dataframe, but I would to include all values in the new columns created. For example say I run the following library(reshape2) dataDF <- data.frame( id = 1:6, id2 = c(1,2,3,1,2,3), x = c(rep('t1', 3), rep('t2', 3)), y = factor(c('A', 'B', 'A', 'B', 'B', 'C'), levels = c('A', 'B', 'C', 'D')), value = rep(1) ) dcast(dataDF, id + id2 ~ x + y, fill = 0) I get the

Reformat data from long to wide

时间秒杀一切 提交于 2019-12-10 16:05:16
问题 How can I reformat this data to a wide format? species val price setosa 5.1 3 setosa 4.9 3 setosa 4.7 3 setosa 4.6 2 Desired output: species val1 val2 val3 val4 price1 price2 price3 price4 setosa 5.1 4.9 4.7 4.6 3 3 3 2 I have tried spread from tidyr but without success. 回答1: data.table v 1.9.6+ allows you to pass more than one value.vars , so you can simply do library(data.table) dcast(setDT(df), species ~ val + price, value.var = c("val", "price")) # species val.1_4.6_2 val.1_4.7_3 val.1_4