reshape2

R: Efficiently remove singleton dimensions from array

社会主义新天地 提交于 2019-12-01 17:54:01
I am looking for a fast way to remove redundant dimensions from an array in R, similar to the squeeze() command in MATLAB. Right now I combine the melt() and the cast() commands from the reshape2 package, but there should be a less intricate way of doing the same. This is how I do it so far: require(reshape2) array3d <- array(rep(0,4),dim=c(1,2,2)) # create a 2*2 matrix within a 3-d array acast(melt(array3d),Var2~Var3) # recover the matrix It sounds like you're looking for drop() , which "delete[s] the dimensions of an array which have only one level". drop(array3d) # [,1] [,2] # [1,] 0 0 # [2

Duplicating observations of a dataframe, but also replacing specific variable values in R

徘徊边缘 提交于 2019-12-01 16:41:25
I am looking for some advice on some data restructuring. I am collecting some data using Google Forms which I download as a csv file and looks something like the following: # alpha beta option # 6 8, 9, 10, 11 apple # 9 6 pear # 1 6 apple # 3 8, 9 pear # 3 6, 8 lime # 3 1 apple # 2, 4, 7, 11 9 lime The data has two variables (alpha and beta) that each list numbers. For the majority of my data there is only one number in each variable. However, for some observations there can be two, three or even up to ten numbers. This is because these are responses gathered using the 'checkbox' option in

R: Efficiently remove singleton dimensions from array

萝らか妹 提交于 2019-12-01 16:22:28
问题 I am looking for a fast way to remove redundant dimensions from an array in R, similar to the squeeze() command in MATLAB. Right now I combine the melt() and the cast() commands from the reshape2 package, but there should be a less intricate way of doing the same. This is how I do it so far: require(reshape2) array3d <- array(rep(0,4),dim=c(1,2,2)) # create a 2*2 matrix within a 3-d array acast(melt(array3d),Var2~Var3) # recover the matrix 回答1: It sounds like you're looking for drop() , which

dcast fun.aggregate parameters

孤街醉人 提交于 2019-12-01 16:15:33
问题 In the R package reshape2 , does the dcast() function parameter fun.aggregate= have the ability to accept parameters itself? For instance: dcast(dataFrame, x ~ y, value.var = 'z', fun.aggregate = mean(na.rm = TRUE)) I'm asking because I use my own function for the fun.aggregate parameter, and I'd rather not hard code the parameters into a growing list of functions. This website is great; thanks everyone. 回答1: Like many functions in R, dcast has a ... argument that is typically used to pass

Solidifying a melted data frame?

微笑、不失礼 提交于 2019-12-01 05:39:27
Assuming I have a melted data.frame that looks like this: variable value 1 A -0.19933093 2 A -1.19043346 3 A -1.32248172 4 A -1.98644507 5 A -0.07930953 6 B -0.10074686 7 B 0.72451483 8 B -0.40914044 9 B 0.02913376 10 B 0.16062491 How do I get it to: A B -0.19933093 -0.10074686 -1.19043346 0.72451483 -1.32248172 -0.40914044 -1.98644507 0.02913376 -0.07930953 0.16062491 Seems trivial but I am blanking on an answer. dcast and acast do not seem to do it. My goal is to do this on larger datasets and turn the end product into a matrix with the column names to be the variable names. I tried playing

Use put two value columns in spread() function in R [duplicate]

橙三吉。 提交于 2019-12-01 04:41:20
This question already has an answer here: Transpose / reshape dataframe without “timevar” from long to wide format 6 answers Convert data from long format to wide format with multiple measure columns 5 answers I just posted a question recently asking how to reshape data from a long table to a wide table. Then I found spread() is a quite handy function for doing this. So now I need some further development on my previous post. Let's suppose we have a table like this: id1 | id2 | info | action_time | action_comment | 1 | a | info1 | time1 | comment1 | 1 | a | info1 | time2 | comment2 | 1 | a |

Solidifying a melted data frame?

倾然丶 夕夏残阳落幕 提交于 2019-12-01 02:37:28
问题 Assuming I have a melted data.frame that looks like this: variable value 1 A -0.19933093 2 A -1.19043346 3 A -1.32248172 4 A -1.98644507 5 A -0.07930953 6 B -0.10074686 7 B 0.72451483 8 B -0.40914044 9 B 0.02913376 10 B 0.16062491 How do I get it to: A B -0.19933093 -0.10074686 -1.19043346 0.72451483 -1.32248172 -0.40914044 -1.98644507 0.02913376 -0.07930953 0.16062491 Seems trivial but I am blanking on an answer. dcast and acast do not seem to do it. My goal is to do this on larger datasets

Use put two value columns in spread() function in R [duplicate]

与世无争的帅哥 提交于 2019-12-01 02:00:38
问题 This question already has answers here : Transpose / reshape dataframe without “timevar” from long to wide format (6 answers) Convert data from long format to wide format with multiple measure columns (5 answers) Closed last year . I just posted a question recently asking how to reshape data from a long table to a wide table. Then I found spread() is a quite handy function for doing this. So now I need some further development on my previous post. Let's suppose we have a table like this: id1

tidyr::gather vs. reshape2::melt on matrices

房东的猫 提交于 2019-11-30 23:45:06
问题 I've been a long time user of reshape2::melt in a rather non-standard way: I'm running numeric experiments and get a matrix as a result. I then melt it and produce some images. Inspired by the similarity between reshape2 and tidyr , I'm now trying to achieve identical output on objects of class matrix. No luck so far: library(reshape2) library(tidyr) set.seed(42) mat <- matrix(runif(6), 3) mat2 <- mat colnames(mat2) <- letters[1:2] rownames(mat2) <- letters[3:5] melt(mat) melt(mat2) gather

melt a data.table with a column pattern

时光怂恿深爱的人放手 提交于 2019-11-30 22:06:23
I have a data.table that looks like this: id A1g_hi A2g_hi A3g_hi A4g_hi 1 2 3 4 5 ... I would like to melt this table so that it looks like this: id time hi 1 1 2 1 2 3 1 3 4 1 4 5 ... I attempted something like this: melt(dtb, measure.vars = patterns("^A"), value.name = "hi", variable.name="time") which does not give me what I would like. Do I need to resort to string splitting here or are there native data.table functions that do this? 42- I raise my glass to @rawr who apparently understands the base-R reshape -function. For me it is an eternal mystery, despite many efforts at understanding