reshape2

reshape2: multiple results of aggregation function?

爱⌒轻易说出口 提交于 2020-01-29 03:07:47
问题 From what I read, *cast operations in reshape2 lost their result_variable feature. Hadley hints at using plyr for this purpose (appending multiple result columns to the input data frame). How would I realize the documentation example ... aqm <- melt(airquality, id=c("month", "day"), na.rm=TRUE) cast(aqm, month ~ variable + result_variable, range) using reshape2 ( dcast ) and plyr ( ddply )? 回答1: This question has multiple answers, due to the flexibility of the 'reshape2' and 'plyr' packages.

reshape2: multiple results of aggregation function?

孤街浪徒 提交于 2020-01-29 03:07:05
问题 From what I read, *cast operations in reshape2 lost their result_variable feature. Hadley hints at using plyr for this purpose (appending multiple result columns to the input data frame). How would I realize the documentation example ... aqm <- melt(airquality, id=c("month", "day"), na.rm=TRUE) cast(aqm, month ~ variable + result_variable, range) using reshape2 ( dcast ) and plyr ( ddply )? 回答1: This question has multiple answers, due to the flexibility of the 'reshape2' and 'plyr' packages.

R: reshaping wide to long [duplicate]

与世无争的帅哥 提交于 2020-01-28 11:21:46
问题 This question already has answers here : Reshaping multiple sets of measurement columns (wide format) into single columns (long format) (7 answers) Closed 2 years ago . 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

R List of lists to dataframe with list name as extra column

眉间皱痕 提交于 2020-01-24 05:18:05
问题 I have a list of lists that have names. I want to add them all together into a dataframe but keep all the columns past_earnings_lists[1] successfully returns one list from the list of lists names(past_earnings_lists)[1] successfully returns the name of the list past_earnings <- melt(past_earnings_lists) puts all the data in one data frame but doesn't keep structure past_earnings <- as.data.frame.matrix(past_earnings_lists$ADBE) successfully takes one list and keeps the structure but doesn't

Reshaping 2 column data.table from long to wide

送分小仙女□ 提交于 2020-01-16 19:37:09
问题 This is my data.frame: library(data.table) df<- fread(' predictions Label 3 A 4 B 5 C 1 A 2 B 3 C ') Desired Output: A B C 3 4 5 1 2 3 I am trying DesiredOutput<-dcast(df, Label+predictions ~ Label, value.var = "predictions") with no success. Your help is appreciated! 回答1: Maybe the base R function unstack is the cleanest solution: unstack(df) A B C 1 3 4 5 2 1 2 3 Note that this returns a data.frame rather than a data.table, so if you want a data.table at the end: df2 <- setDT(unstack(df))

could not find function “cast” despite reshape2 installed and loaded

≯℡__Kan透↙ 提交于 2020-01-14 07:10:26
问题 In following Hadley Wickham's "Practical tools for exploring data and models" examples in section 2.4, Casting molten data I'm getting Error: could not find function "cast" . I have the reshape2 package installed and loaded. > cast(msmithsm, time + subject ~ variable) Error: could not find function "cast" 回答1: In reshape2 , cast() has been replaced by two functions: acast() and dcast() . From ?cast , in package reshape2 : Description: Use ‘acast’ or ‘dcast’ depending on whether you want

could not find function “cast” despite reshape2 installed and loaded

ε祈祈猫儿з 提交于 2020-01-14 07:10:08
问题 In following Hadley Wickham's "Practical tools for exploring data and models" examples in section 2.4, Casting molten data I'm getting Error: could not find function "cast" . I have the reshape2 package installed and loaded. > cast(msmithsm, time + subject ~ variable) Error: could not find function "cast" 回答1: In reshape2 , cast() has been replaced by two functions: acast() and dcast() . From ?cast , in package reshape2 : Description: Use ‘acast’ or ‘dcast’ depending on whether you want

Reshape package masking preventing melt from naming columns

谁说胖子不能爱 提交于 2020-01-12 22:30:50
问题 I have a script which requires both reshape and reshape2 libraries. I know this is poor practise, but I think plyr (or another library I am using) Vennerable is loading reshape and I have personally used reshape2 in a lot of places. The problem is that the masking of reshape2 by reshape is causing problems for the melt function # Example data frame df <- data.frame(id=c(1:5), a=c(rnorm(5)), b=c(rnorm(5))) # With just reshape2, variable and value columns are labelled correctly library(reshape2

Removing duplicate rows from data frame in R

本秂侑毒 提交于 2020-01-10 04:26:07
问题 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 回答1: We can use data

Spread vs dcast

醉酒当歌 提交于 2020-01-09 19:35:52
问题 I have a table like this, > head(dt2) Weight Height Fitted interval limit value 1 65.6 174.0 71.91200 pred lwr 53.73165 2 80.7 193.5 91.63237 pred lwr 73.33198 3 72.6 186.5 84.55326 pred lwr 66.31751 4 78.8 187.2 85.26117 pred lwr 67.02004 5 74.8 181.5 79.49675 pred lwr 61.29244 6 86.4 184.0 82.02501 pred lwr 63.80652 I want it to have like this, > head(reshape2::dcast(dt2, Weight + Height + Fitted + interval ~ limit, fun.aggregate = mean)) Weight Height Fitted interval lwr upr 1 42.0 153.4