reshape2

Reshaping data with count [duplicate]

ε祈祈猫儿з 提交于 2019-11-29 11:47:40
This question already has an answer here: Faster ways to calculate frequencies and cast from long to wide 4 answers 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 ... C D ... ... This is my code: dcast(dados_teste, cat_one ~ customer, value.var = 'valor') And I'm following this other

dcast without ID variables

余生长醉 提交于 2019-11-29 11:36:56
In the "An Introduction to reshape2" package Sean C. Anderson presents the following example. He uses the airquality data and renames the column names names(airquality) <- tolower(names(airquality)) The data look like # ozone solar.r wind temp month day # 1 41 190 7.4 67 5 1 # 2 36 118 8.0 72 5 2 # 3 12 149 12.6 74 5 3 # 4 18 313 11.5 62 5 4 # 5 NA NA 14.3 56 5 5 # 6 28 NA 14.9 66 5 6 Then he melts them by aql <- melt(airquality, id.vars = c("month", "day")) to get # month day variable value # 1 5 1 ozone 41 # 2 5 2 ozone 36 # 3 5 3 ozone 12 # 4 5 4 ozone 18 # 5 5 5 ozone NA # 6 5 6 ozone 28

segfault in R using reshape2 package and dcast

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 09:31:08
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 my data frame is about 558,000 rows and the problem doesn't occur on small toy examples. For example,

Adding Percentages to a Grouped Barchart Columns in GGplot2

时间秒杀一切 提交于 2019-11-29 08:47:49
Hoping someone can help me with labelling columns of a grouped barchart with percentages. I couldn't find an existing post that I could make work successfuly. Below is the code for a basic example dataframe. Service<-c("AS","AS","PS","PS","RS","RS","ES","ES") Year<-c("2015","2016","2015","2016","2015","2016","2015","2016") Q1<-c("Dissatisfied","Satisfied","Satisfied","Satisfied","Dissatisfied","Dissatisfied","Satisfied","Satisfied") Q2<-c("Dissatisfied","Dissatisfied","Satisfied","Dissatisfied","Dissatisfied","Satisfied","Satisfied","Satisfied") Example<-data.frame(Service,Year,Q1,Q2) Next, I

How to “unmelt” data with reshape r

ぃ、小莉子 提交于 2019-11-29 07:39:03
问题 I have a data frame that I melted using the reshape package that I would like to "un melt". here is a toy example of the melted data (real data frame is 500x100 or larger) : variable<-c(rep("X1",3),rep("X2",3),rep("X3",3)) value<-c(rep(rnorm(1,.5,.2),3),rep(rnorm(1,.5,.2),3),rep(rnorm(1,.5,.2),3)) dat <-data.frame(variable,value) dat variable value 1 X1 0.5285376 2 X1 0.5285376 3 X1 0.5285376 4 X2 0.1694908 5 X2 0.1694908 6 X2 0.1694908 7 X3 0.7446906 8 X3 0.7446906 9 X3 0.7446906 Each

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

ε祈祈猫儿з 提交于 2019-11-29 07:27: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 bounds In addition: Warning message: In if (!(value.var %in% names(data))) { : the condition has length

Using melt with matrix or data.frame gives different output

こ雲淡風輕ζ 提交于 2019-11-29 07:04:57
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) variable value 1 A -0.6264538 2 A 0.1836433 3 A -0.8356286 4 B 1.5952808 5 B 0.3295078 6 B -0.8204684 7 C

No non-missing arguments warning when using min or max in reshape2

三世轮回 提交于 2019-11-29 04:50:16
问题 I get the following warning when I use min or max in the dcast function from the reshape2 package. What is it telling me? I can't find anything that explains the warning message and I'm a bit confused about why I get it when I use max but not when I use mean or other aggregate functions. Warning message: In .fun(.value[0], ...) : no non-missing arguments to min; returning Inf Here's a reproducible example: data(iris) library(reshape2) molten.iris <- melt(iris,id.var="Species") summary(molten

R: “Unary operator error” from multiline ggplot2 command

限于喜欢 提交于 2019-11-29 04:44:16
问题 I'm using ggplot2 to do a boxplot comparison of two different species, as indicated by the third column shown below: > library(reshape2) > library(ggplot2) > melt.data = melt(actb.raw.data) > head(actb.raw.data) region expression species 1 CG -0.17686667 human 2 CG -0.06506667 human 3 DG 1.04590000 human 4 CA1 1.94093333 human 5 CA2 1.55023333 human 6 CA3 1.75800000 human > head(melt.data) region species variable value 1 CG human expression -0.17686667 2 CG human expression -0.06506667 3 DG

R reshape2 'Aggregation function missing: defaulting to length' [duplicate]

て烟熏妆下的殇ゞ 提交于 2019-11-29 03:09:08
This question already has an answer here: dcast error: ‘Aggregation function missing: defaulting to length’ 1 answer I have seen this reshape2 several times on SO but haven't seen a solution to my particular problem; I have a dataset like this; head(data) student test score Adam Exam1 80 Adam Exam2 90 John Exam1 70 John Exam2 60 I am trying to cast this to a wide format that looks like this; Student Exam1 Exam2 ........ ExamX Adam 80 90 John 70 60 using; dcast(data,student~test,value.var='score') but the data ends up looking like something like this; Student Exam1 Exam2 Adam 0 0 John 0 1 with