I realize that this is terribly basic, but I just don't seem to see it at this moment, so I would very much appreciate your help. How shall I transform this dataframe:> df1? Name Index Value 1??? a???? 1?? 0.1 2??? a???? 2?? 0.2 3??? a???? 3?? 0.3 4??? a???? 4?? 0.4 5??? b???? 1?? 2.1 6??? b???? 2?? 2.2 7??? b???? 3?? 2.3 8??? b???? 4?? 2.4 into this dataframe:> df2??? Index? a?? ??? b 1? 1 ??? 0.1 ??? 2.1 2? 2 ??? 0.2 ??? 2.2 3? 3 ??? 0.3 ??? 2.3 4? 4 ??? 0.4 ??? 2.4 df1 = data.frame(c("a", "a", "a", "a", "b", "b", "b", "b"), c(1,2,3,4,1,2,3,4), c(0.1, 0.2, 0.3, 0.4, 2.1, 2.2, 2.3, 2.4)) colnames(df1) = c("Name", "Index", "Value") df2 = data.frame(c(1,2,3,4), c(0.1, 0.2, 0.3, 0.4), c(2.1, 2.2, 2.3, 2.4)) colnames(df2) = c("Index", "a", "b") Thank you very much. Dana
df2<-melt(df1) df3<-cast(df2,Index~Name) df3 HTH, Daniel Dana Sevak wrote:> > I realize that this is terribly basic, but I just don't seem to see it at > this moment, so I would very much appreciate your help. > > > How shall I transform this dataframe: > >> df1 > ? Name Index Value > 1??? a???? 1?? 0.1 > 2??? a???? 2?? 0.2 > 3??? a???? 3?? 0.3 > 4??? a???? 4?? 0.4 > 5??? b???? 1?? 2.1 > 6??? b???? 2?? 2.2 > 7??? b???? 3?? 2.3 > 8??? b???? 4?? 2.4 > > > into this dataframe: > >> df2 > ??? Index? a?? ??? b > 1? 1 ??? 0.1 ??? 2.1 > 2? 2 ??? 0.2 ??? 2.2 > 3? 3 ??? 0.3 ??? 2.3 > 4? 4 ??? 0.4 ??? 2.4 > > > df1 = data.frame(c("a", "a", "a", "a", "b", "b", "b", "b"), > c(1,2,3,4,1,2,3,4), c(0.1, 0.2, 0.3, 0.4, 2.1, 2.2, 2.3, 2.4)) > colnames(df1) = c("Name", "Index", "Value") > > df2 = data.frame(c(1,2,3,4), c(0.1, 0.2, 0.3, 0.4), c(2.1, 2.2, 2.3, 2.4)) > colnames(df2) = c("Index", "a", "b") > > > Thank you very much. > > Dana > > > ______________________________________________ > R-help@ mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- View this message in context: http://r.789695.n4.nabble.com/Help-with-cast-reshape-tp3862176p3862404.html Sent from the R help mailing list archive at Nabble.com.
Hi> > I realize that this is terribly basic, but I just don't seem to see itat> this moment, so I would very much appreciate your help. > > > How shall I transform this dataframe: > > > df1 > Name Index Value > 1 a 1 0.1 > 2 a 2 0.2 > 3 a 3 0.3 > 4 a 4 0.4 > 5 b 1 2.1 > 6 b 2 2.2 > 7 b 3 2.3 > 8 b 4 2.4 > > > into this dataframe: > > > df2 > Index a b > 1 1 0.1 2.1 > 2 2 0.2 2.2 > 3 3 0.3 2.3 > 4 4 0.4 2.4 >I have not seen an answer so I believe you look for: cast(df1, Index~Name, value = "Value") Regards Petr> > df1 = data.frame(c("a", "a", "a", "a", "b", "b", "b", "b"),c(1,2,3,4,1,2,> 3,4), c(0.1, 0.2, 0.3, 0.4, 2.1, 2.2, 2.3, 2.4)) > colnames(df1) = c("Name", "Index", "Value") > > df2 = data.frame(c(1,2,3,4), c(0.1, 0.2, 0.3, 0.4), c(2.1, 2.2, 2.3,2.4))> colnames(df2) = c("Index", "a", "b") > > > Thank you very much. > > Dana > > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Maybe Matching Threads
- Help with reshape/reShape and indexing
- Best way/practice to create a new data frame from two given ones with last column computed from the two data frames?
- merging several dataframes from a list
- Looping through a list of objects & do something...
- indexing into a data.frame using another data.frame that also contains values for replacement