Error In .data.frame Data Timevar Undefined Columns Selected
Contents |
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions reshape r long to wide Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a
R Reshape Function
community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up How to reshape this dataframe with
Dcast R
the reshape package up vote 8 down vote favorite 4 I have a quite large dataframe structured like this: id x1 x2 x3 y1 y2 y3 z1 z2 z3 v 1 2 4 5 10 20 15 200 150 170 2.5 2 3 7
R Melt
6 25 35 40 300 350 400 4.2 I need to create a dataframe like this: id xsource xvalue yvalue zvalue v 1 x1 2 10 200 2.5 1 x2 4 20 150 2.5 1 x3 5 15 170 2.5 2 x1 3 25 300 4.2 2 x2 7 35 350 4.2 2 x3 6 40 400 4.2 I'm quite sure I have to do it with the reshape package, but I'm not able to get what I want. Could you help me? Thanks r reshape share|improve this question divar asked Jan 13 '12 at 15:57 corrado 7528 add a comment| 4 Answers 4 active oldest votes up vote 7 down vote accepted Here's the reshape() solution. The key bit is that the varying= argument can take a list of vectors of column names in the wide format that correspond to single variables in the long format. In this case, columns "x1", "x2", "x3" in the original data frame are sent to one column in the long data frame, columns "y1, y2, y3" will go into a second column, and so on. # Read in the original data, x, from Andrie's answer res <- reshape(x, direction = "long", idvar = "id", varying = list(c("x1","x2", "x3"), c("y1", "y2", "y3"), c("z1", "z2", "z3")), v.names = c("xvalue", "yvalue", "zvalue"), timevar = "xsource", times = c("x1", "x2", "x3")) # id v xsource xvalue yvalue zvalue # 1.x1 1 2.5 x1 2 10 200 # 2.x1 2 4.2 x1 3 25 300 # 1.x2 1 2.5 x2 4 20 150 # 2.x2 2 4.2 x2 7 35 350 # 1.x3 1 2.5 x3 5 15 170 # 2.x3 2 4.2 x3 6 40 400 Finally, a couple of purely cosmetic steps are needed to get the results looking exactly as shown in your question: res <- res[order(res$id, res$xsource), c(1,3,4,5,6,2)] row.names(res) <- NULL res # id xsource xvalue yvalue zvalue v # 1 1 x1 2 10 200 2.5 # 2 1 x2 4 20 150 2.5 # 3 1 x3 5 15 170 2.5 # 4 2 x1 3 25 300 4.2 # 5 2 x2 7
♦ ♦ | Report Content as Inappropriate ♦ ♦ Reshape from long to wide Hi, I'm a total beginner in R and this question is probably very simple but I've spent hours reading about it and can't find the answer. I'm trying to reshape a data table from long to wide format. I've tried reshape() and cast() but I get error messages every time and I can't figure why. In my data, I have the length of two fish from each family. My data table (called fish) looks like this: family length 14 18 14 7 15 7 15 21 17 50 17 21 18 36 18 21 20 36 20 42 24 56 24 42 25 43 25 56 27 15 27 42 28 7 28 42 29 56 29 49 I want it to look like this: family kid1 kid2 14 18 7 15 7 21 17 50 21 18 36 21 28 36 42 24 56 42 25 43 56 27 http://stackoverflow.com/questions/8853364/how-to-reshape-this-dataframe-with-the-reshape-package 15 42 28 7 42 29 56 49 I've tried: >cast( fish, fam~length) and got the error message: Using length as value column. Use the value argument to cast to override this choice Error in `[.data.frame`(data, , variables, drop = FALSE) : undefined columns selected Then I rename the columns: >myvars<-c("fam","length") >fish<-fish[myvars] and try the cast() again with no luck (same error) By using reshape() I don't get the results I want: >reshape(rdm1, timevar="fam", idvar=c("length"), direction="wide") > head(first) length 14.20 14 14.19 7 15.25 21 http://r.789695.n4.nabble.com/Reshape-from-long-to-wide-td4486875.html 17.30 50 18.32 36 20.36 42 Can someone help with this? Thanks a lot! Jorge I Velez Threaded Open this post in threaded view ♦ ♦ | Report Content as Inappropriate ♦ ♦ Re: Reshape from long to wide Hi aly, Try # your data x <- structure(list(family = c(14L, 14L, 15L, 15L, 17L, 17L, 18L, 18L, 20L, 20L, 24L, 24L, 25L, 25L, 27L, 27L, 28L, 28L, 29L, 29L ), length = c(18L, 7L, 7L, 21L, 50L, 21L, 36L, 21L, 36L, 42L, 56L, 42L, 43L, 56L, 15L, 42L, 7L, 42L, 56L, 49L)), .Names = c("family", "length"), class = "data.frame", row.names = c(NA, -20L)) # processing require(plyr) ddply(x, .(family), function(df) c(kid1 = df$length[1], kid2 = df$length[2])) family kid1 kid2 1 14 18 7 2 15 7 21 3 17 50 21 4 18 36 21 5 20 36 42 6 24 56 42 7 25 43 56 8 27 15 42 9 28 7 42 10 29 56 49 HTH, Jorge.- On Mon, Mar 19, 2012 at 7:01 PM, aly <> wrote: > Hi, > > I'm a total beginner in R and this question is probably very simple but > I've > spent hours reading about it and can't find the answer. I'm trying to > reshape a data table from long to wide format. I've tried reshape() and > cast() but I get error messages every time and I can't figure why. In my > data, I have the length of two fish from each family. My data table (called > fish) lo
Open this post in threaded view ♦ ♦ | Report Content as http://r.789695.n4.nabble.com/Error-in-data-frame-undefined-columns-selected-td4650002.html Inappropriate ♦ ♦ Error in `[.data.frame`... undefined columns selected When I run this script on 9 variables, it works without problems. Z <- data[,c("s1_1234_m","s2_1234_m","s3_1234_m","s4_1234_m","s5_1234_m","s6_1234_m","s7_1234_m","s8_1234_m","s9_1234_m" )] However, when I run the script on 9 different variables, it does not work: Z <- data[,c("d_s1_m","d_s2_m","d_s3_m","d_s4_m","d_s5_m","d_s6_m","d_s7_m","d_s8m","d_s9m" )] Error in `[.data.frame`(data, , error in c("d_s1_m", "d_s2_m", "d_s3_m", "d_s4_m", : undefined columns selected The first 9 variables are between 0 and 3, there are no missing values in the dataset. The second 9 variables are between -3 and 3, there are no missing values in the dataset. I am pretty new error in .data.frame to R and have no idea what could cause this error. Thank you [[alternative HTML version deleted]] ______________________________________________ [hidden email] mailing list https://stat.ethz.ch/mailman/listinfo/r-helpPLEASE do read the posting guide http://www.R-project.org/posting-guide.htmland provide commented, minimal, self-contained, reproducible code. David Winsemius Threaded Open this post in threaded view ♦ ♦ | Report Content as Inappropriate ♦ ♦ Re: Error in `[.data.frame`... undefined columns selected On Nov 18, 2012, at 6:34 PM, Eiko Fried wrote: > When I run this script on 9 variables, it works without problems. > > Z <- > data > [,c > ("s1_1234_m > ","s2_1234_m > ","s3_1234_m > ","s4_1234_m > ","s5_1234_m","s6_1234_m","s7_1234_m","s8_1234_m","s9_1234_m" > )] > > However, when I run the script on 9 different variables, it does not > work: > Z <- > data > [,c > ("d_s1_m > ","d_s2_m > ","d_s3_m","d_s4_m","d_s5_m","d_s6_m","d_s7_m",