reshape2

Convert from n x m matrix to long matrix in R

…衆ロ難τιáo~ 提交于 2019-11-28 07:24:22
问题 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?

How to split one column into different columns with dcast without aggregating?

喜夏-厌秋 提交于 2019-11-28 06:58:54
问题 I'm trying to reshape my data using dcast. I'm working with samples where each sample has 10-30 sample units. I can't have my data aggregate. My data is in this format: ID total sample_1 1 sample_1 0 sample_1 2 sample_1 1 sample_1 0 sample_1 0 sample_1 2 sample_1 1 sample_1 0 sample_1 2 sample_1 1 sample_1 4 sample_2 2 sample_2 1 sample_2 2 sample_2 0 sample_2 0 sample_2 0 sample_2 1 sample_2 2 sample_2 1 sample_2 4 sample_2 5 sample_2 2 sample_2 1 sample_3 0 sample_3 0 sample_3 1 sample_3 2

Reshape data from long to wide format - more than one variable [duplicate]

落花浮王杯 提交于 2019-11-28 06:47:13
问题 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: +---------+------+--------+---------

Reshaping data with count [duplicate]

最后都变了- 提交于 2019-11-28 05:22:16
问题 This question already has answers here : Faster ways to calculate frequencies and cast from long to wide (4 answers) Closed last year . I have a dataset and I want to reshape it with package reshape2 from R, but I'm getting this error: Aggregation function missing: defaulting to length This is the head() of my data: cat_one customer valor cama A 1 cama B 1 cama C 1 mesa D 1 mesa A 1 mesa A 1 And I want to reshape it like this, with a count between both variables: customer cama mesa A 1 0 B 2

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

心不动则不痛 提交于 2019-11-28 05:07:23
问题 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

segfault in R using reshape2 package and dcast

…衆ロ難τιáo~ 提交于 2019-11-28 03:05:04
问题 RStudio was crashing when I tried to reshape a particular data frame using dcast (from the reshape2 package). I discovered that the crash was actually happening in R itself, so I ran my casting code in R.app and got the type of error that gives this site its name: Error: segfault from C stack overflow . With the help of Google and SO, I learned that this is a memory access error. Okay, I got that far, but I don't know where to go from here. I can't provide a true reproducible example, because

Tidy data.frame with repeated column names

99封情书 提交于 2019-11-28 01:51:23
I have a program that gives me data in this format toy file_path Condition Trial.Num A B C ID A B C ID A B C ID 1 root/some.extension Baseline 1 2 3 5 car 2 1 7 bike 4 9 0 plane 2 root/thing.extension Baseline 2 3 6 45 car 5 4 4 bike 9 5 4 plane 3 root/else.extension Baseline 3 4 4 6 car 7 5 4 bike 68 7 56 plane 4 root/uniquely.extension Treatment 1 5 3 7 car 1 7 37 bike 9 8 7 plane 5 root/defined.extension Treatment 2 6 7 3 car 4 6 8 bike 9 0 8 plane My goal is to tidy the format into something that at least can be easier to finally tidy with reshape having unique column names tidy_toy file

Tidyr how to spread into count of occurrence [duplicate]

☆樱花仙子☆ 提交于 2019-11-28 01:20:02
This question already has an answer here: How do I get a contingency table? 6 answers Faster ways to calculate frequencies and cast from long to wide 4 answers Have a data frame like this other=data.frame(name=c("a","b","a","c","d"),result=c("Y","N","Y","Y","N")) How can I use spread function in tidyr or other function to get the count of result Y or N as column header like this name Y N a 2 0 b 0 1 Thanks These are a few ways of many to go about it: 1) With library dplyr , you can simply group things and count into the format needed: library(dplyr) other %>% group_by(name) %>% summarise(N =

Why can't one have several `value.var` in `dcast`?

非 Y 不嫁゛ 提交于 2019-11-28 01:01:56
问题 Why can't one have multiple variables passed to value.var in dcast ? From ?dcast : value.var name of column which stores values, see guess_value for default strategies to figure this out. It doesn't explicitly indicate that only one single variable can be passed on as value. If however I try that, then I get an error: > library("reshape2") > library("MASS") > > dcast(Cars93, AirBags ~ DriveTrain, mean, value.var=c("Price", "Weight")) Error in .subset2(x, i, exact = exact) : subscript out of

Using melt with matrix or data.frame gives different output

不想你离开。 提交于 2019-11-28 00:37:30
问题 Consider the following code: set.seed(1) M = matrix(rnorm(9), ncol = 3) dimnames(M) = list(LETTERS[1:3], LETTERS[1:3]) print(M) A B C A -0.6264538 1.5952808 0.4874291 B 0.1836433 0.3295078 0.7383247 C -0.8356286 -0.8204684 0.5757814 melt(M) Var1 Var2 value 1 A A -0.6264538 2 B A 0.1836433 3 C A -0.8356286 4 A B 1.5952808 5 B B 0.3295078 6 C B -0.8204684 7 A C 0.4874291 8 B C 0.7383247 9 C C 0.5757814 If i call melt using a data.frame , i get a different result: DF = data.frame(M) melt(DF)