reshape2

How to use a character vector of column names in the formula argument of dcast (reshape2)

十年热恋 提交于 2020-01-09 11:23:48
问题 Say I have a dataframe df with dozens of identifying variables (in columns) and only a few measured variables (also in columns). To avoid repetitively typing all the variables for each argument, I assign the names of the identifying and measured df columns to df_id and df_measured , respectively. It's easy enough to input these vectors to shorten the argument inputs for melt ... df.m <- melt(df, id.vars = df_id, measure.vars = df_measured) ... but I'm at a loss for how to enter the formula =

Reshape Data Long to Wide - understanding reshape parameters

久未见 提交于 2020-01-09 05:32:25
问题 I have a long format dataframe dogs that I'm trying to reformat to wide using the reshape() function. It currently looks like so: dogid month year trainingtype home school timeincomp 12345 1 2014 1 1 1 340 12345 2 2014 1 1 1 360 31323 12 2015 2 7 3 440 31323 1 2014 1 7 3 500 31323 2 2014 1 7 3 520 The dogid column is a bunch of ids, one for each dog. The month column varies for 1 to 12 for the 12 months, and year from 2014 to 2015. Trainingtype varies for 1 to 2. Each dog has a timeincomp

reshape2 melt not producing all pairs

牧云@^-^@ 提交于 2020-01-07 03:48:04
问题 I have a 332 x 332 distance matrix with distances between all possible pairs of stations. I need to transform this matrix into a data.frame with three columns - start.id, end.id, and distance. I have tried the melt function from the reshape2 package but this is not giving the right result Here is a snippet of the data mat 72 79 82 83 116 119 120 127 128 137 143 144 146 147 150 151 152 153 157 160 161 72 NA 6.43 7.458 11.546 3.784 10.252 12.780 4.835 5.230 2.168 10.614 9.806 7.058 6.533 7.117

Transforming a min() and a max() value into a range of values

|▌冷眼眸甩不掉的悲伤 提交于 2020-01-06 18:34:28
问题 I would like stretch some of my records that were flattened I have a table like this Store Min(Date) Max (Date) Status NYC1 1/1/2013 2/1/2013 Open NYC1 2/2/2013 2/3/2013 Closed for Inspection Boston1 1/1/2013 2/5/2013 Open and I would like to stretch it into the form Store Date Status NYC1 1/1/2013 Open NYC1 1/2/2013 Open ..... NYC1 2/2/2013 Closed for Inspection NYC1 2/3/2013 Closed for Inspection .... Boston1 1/1/2013 Open I know I can always write loops for this, but before attempting so,

How to melt and plot multiple datasets over different ranges on the same set of axis?

梦想与她 提交于 2020-01-06 05:45:10
问题 This is my first time posting here, I hope my question is clear and appropriate. I have a set of data the head of which looks like this: wl ex421 wl ex309 wl ex284 wl ex347 1 431 0.6168224 321 0.1267943 301 0.06392694 361 0.15220484 2 432 0.6687435 322 0.2416268 302 0.05631659 362 0.08961593 3 433 0.6583593 323 0.4665072 303 0.05327245 363 0.13134187 4 434 0.6832814 324 0.3576555 304 0.00000000 364 0.32432432 5 435 0.6427830 325 0.2194976 305 0.12328767 365 0.50308203 6 436 0.7393562 326 0

How to strsplit data frame column and replicate rows accordingly? [duplicate]

老子叫甜甜 提交于 2020-01-03 10:19:08
问题 This question already has answers here : Split comma-separated strings in a column into separate rows (5 answers) Closed 3 years ago . I have a data frame like this: > df <- data.frame(Column1=c("id1", "id2", "id3"), Column2=c("text1,text2,text3", "text4", "text5,text6"), Column3=c("text7", "text8,text9,text10,text11", "text12,text13")) > df Column1 Column2 Column3 1 id1 text1,text2,text3 text7 2 id2 text4 text8,text9,text10,text11 3 id3 text5,text6 text12,text13 How do I transform it in this

Data manipulation with forecasting data

烂漫一生 提交于 2020-01-03 04:33:49
问题 I am trying to make forecasting for sales from two stores: Store 1 and Store 2. Like results from forecasting with forecasting package I got this two table.First table contain data about MAPE error separably by each model(column Value).Below you can see data and screen shot of data. Table_1<-structure(list(...1 = c("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20"), X1 = c("SNAIVE", "HW", "ETS", "ARIMA", "STL", "TBATS", "NNETAR",

Why reshape2's Melt cannot capture rownames in the transformation?

倖福魔咒の 提交于 2019-12-31 10:04:40
问题 I have the following data: Cubn 3.71455160837536 0.237454645363458 Gm9779 2.56051657980096 0.20850752817264 Apod 3.51796703048962 0.195999214485821 What I want to do is to create the 'melted' data such that it gives this var1 var2 value 1 FOO Cubn 3.7145516 2 FOO Gm9779 2.5605166 3 FOO Apod 3.5179670 4 BAR Cubn 0.2374546 5 BAR Gm9779 0.2085075 6 BAR Apod 0.1959992 But why this failed? library("reshape2"); dat <-read.table("http://dpaste.com/1446132/plain/",header=FALSE) rownames(dat) <- dat[

Why reshape2's Melt cannot capture rownames in the transformation?

倖福魔咒の 提交于 2019-12-31 10:04:34
问题 I have the following data: Cubn 3.71455160837536 0.237454645363458 Gm9779 2.56051657980096 0.20850752817264 Apod 3.51796703048962 0.195999214485821 What I want to do is to create the 'melted' data such that it gives this var1 var2 value 1 FOO Cubn 3.7145516 2 FOO Gm9779 2.5605166 3 FOO Apod 3.5179670 4 BAR Cubn 0.2374546 5 BAR Gm9779 0.2085075 6 BAR Apod 0.1959992 But why this failed? library("reshape2"); dat <-read.table("http://dpaste.com/1446132/plain/",header=FALSE) rownames(dat) <- dat[

reshape from base vs dcast from reshape2 with missing values

放肆的年华 提交于 2019-12-31 02:45:32
问题 Whis this data frame, df <- expand.grid(id="01", parameter=c("blood", "saliva"), visit=c("V1", "V2", "V3")) df$value <- c(1:6) df$sex <- rep("f", 6) df > df id parameter visit value sex 1 01 blood V1 1 f 2 01 saliva V1 2 f 3 01 blood V2 3 f 4 01 saliva V2 4 f 5 01 blood V3 5 f 6 01 saliva V3 6 f When I reshape it in the "wide" format, I get identical results with both the base reshape function and the dcast function from reshape2 . reshape(df, timevar="visit", idvar=c("id", "parameter", "sex"