reshape2

Complicated reshaping

爷,独闯天下 提交于 2019-11-30 02:22:26
I want to reshape my dataframe from long to wide format and I loose some data that I'd like to keep. For the following example: df <- data.frame(Par1 = unlist(strsplit("AABBCCC","")), Par2 = unlist(strsplit("DDEEFFF","")), ParD = unlist(strsplit("foo,bar,baz,qux,bla,xyz,meh",",")), Type = unlist(strsplit("pre,post,pre,post,pre,post,post",",")), Val = c(10,20,30,40,50,60,70)) # Par1 Par2 ParD Type Val # 1 A D foo pre 10 # 2 A D bar post 20 # 3 B E baz pre 30 # 4 B E qux post 40 # 5 C F bla pre 50 # 6 C F xyz post 60 # 7 C F meh post 70 dfw <- dcast(df, formula = Par1 + Par2 ~ Type, value.var =

R: reshaping wide to long [duplicate]

时间秒杀一切 提交于 2019-11-29 17:15:34
This question already has an answer here: Reshaping multiple sets of measurement columns (wide format) into single columns (long format) 7 answers I have a wide dataframe that looks something like this: ID Time Amount CabMean CabNum PartMean PartNum DinnMean DinNum Ex 1 1 1 27 0.654621546 8 NA 7 0.316791872 6 0 2 1 2 82 0.667461321 3 0.327594876 4 0.346798127 2 1 3 1 3 52 0.313976132 1 NA 6 0.197837257 7 0 4 1 4 99 0.798328712 9 0.913751678 4 0.191679538 9 1 I would like to reshape (using the reshape2 package) it to a long format that takes this form (just making these numbers up): ID Time

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

落爺英雄遲暮 提交于 2019-11-29 15:42:30
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 Try library(splitstackshape) cSplit(data, 'B', ',', 'long') Or using base R lst <- setNames(strsplit(as.character(data$B), ','), data$A) stack(lst) Or library(tidyr) unnest(lst,A) 来源: https://stackoverflow.com/questions/30818840/in-r-get-multiple

Balancing (creating same number of rows for each individual) data

痞子三分冷 提交于 2019-11-29 15:10:45
Given a data.table as follows, id1 is a subject-level ID, id2 is a within-subject repeated-measure ID, X are data variables of which there are many. I want to balance the data such that every individual has the same number of rows (repeated measures), which is the max(DT[,.N,by=id1][,N]) , but where id1 and id2 are adjusted as necessary, and X data values are replaced with NA for these new rows. The following: DT = data.table( id1 = c(1,1,2,2,2,3,3,3,3), id2 = c(1,2,1,2,3,1,2,3,4), X1 = letters[1:9], X2 = LETTERS[1:9] ) setkey(DT,id1) Should look like: DT = data.table( id1 = c(1,1,1,1,2,2,2,2

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

十年热恋 提交于 2019-11-29 14:55:23
This question already has an answer here: Transpose / reshape dataframe without “timevar” from long to wide format 6 answers 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_Number3 John Doe 0123456 0123457 0123458 Jim Doe 0123459 0123450 NA Jane Doe 0123451 NA NA Jill Doe NA NA NA library(dplyr)

Simpler way to reconstitute a melted data frame back to the original

假如想象 提交于 2019-11-29 13:57:11
How do I recreate a data.frame that I melted with reshape2 ? Reproducible example library(reshape2) library(plyr) data(iris) df <- melt(iris, id.vars="Species") head(df) Species variable value 1 setosa Sepal.Length 5.1 2 setosa Sepal.Length 4.9 3 setosa Sepal.Length 4.7 4 setosa Sepal.Length 4.6 5 setosa Sepal.Length 5.0 6 setosa Sepal.Length 5.4 # Great, I'd like to get the original iris back What I've tried with dcast dcast(df, Species~variable, value.var = "value") # should work but doesn't temporary solution # This works but clearly it shouldn't be this hard. ddply(df, .(Species), function

Convert from n x m matrix to long matrix in R

北战南征 提交于 2019-11-29 13:32:21
Note: This is not a graph question. I have an n x m matrix: > m = matrix(1:6,2,3) > m a b c d 1 2 3 e 4 5 6 I would like to convert this to a long matrix: > m.l a d 1 a e 4 b d 2 b e 5 c d 3 c e 6 Obviously nested for loops would work but I know there are a lot of nice tools for reshaping matrixes in R. So far, I have only found literature on converting from long or wide matrixes to an n x m matrix and not the other way around. Am I missing something obvious? How can I do this conversion? Thank you! If you need a single column matrix matrix(m, dimnames=list(t(outer(colnames(m), rownames(m),

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 | +---------+------+--------

Removing duplicate rows from data frame in R

雨燕双飞 提交于 2019-11-29 12:04:34
I have two columns, would like to retain only the non commutative rows.For the data below my output should contain one combination of (1 2). i.e. for my query (1 2) is same as (2 1). Is there a simple way to do it in R. Already tried transposing. and retaining the upper traingular matrix. but it becomes a pain re transposing back the data. A B prob 1 2 0.1 1 3 0.2 1 4 0.3 2 1 0.3 2 3 0.1 2 4 0.4 My final output should be: A B prob 1 2 0.1 1 3 0.2 1 4 0.3 2 3 0.1 2 4 0.4 We can use data.table . Convert the 'data.frame' to 'data.table' ( setDT(df1) ), grouped by the pmin(A, B) and pmax(A,B) , if

What reshaping problems can melt/cast not solve in a single step?

試著忘記壹切 提交于 2019-11-29 11:55:06
reshape2 is a package which allows an powerful array of data transformations, through its two-part melt/cast approach. However, like all tools it embeds assumptions which limit the cases it can handle. What data reshaping problem can reshape2 not handle in its current form? The ideal answer will include: A description of the type of use cases where this data shape is typically found Sample data Code to accomplish the transformation (ideally using as much of the transformation with reshape2 as possible) Example "Wide" data is common in panel applications. melt.wide <- function(data, id.vars,