Hello! I have a data.frame which looks like: Name - Value A - 400 A - 300 B - 200 B - 350 C - 500 C - 350 D - 450 D - 600 E - 700 E - 750 F - 630 F - 650 I want to add another column where all A,B should get an index 1, all C,D an index of 2 and all E,F an index of 3 so that the data.frame looks like: ID - Name - Value 1 - A - 400 1 - A - 300 1 - B - 200 1 - B - 350 2 - C - 500 2 - C - 350 2 - D - 450 2 - D - 600 3 - E - 700 3 - E - 750 3 - F - 630 3 - F - 650 My data.frame is quite big so I cannot add all values by hand. Cheers -- View this message in context: http://r.789695.n4.nabble.com/Data-alignment-tp4153024p4153024.html Sent from the R help mailing list archive at Nabble.com.
I doubt your data frame looks like that, with all the -, but regardless you can use ifelse() to construct your column. Sarah On Saturday, December 3, 2011, syrvn <mentor_@gmx.net> wrote:> Hello! > > I have a data.frame which looks like: > > Name - Value > A - 400 > A - 300 > B - 200 > B - 350 > C - 500 > C - 350 > D - 450 > D - 600 > E - 700 > E - 750 > F - 630 > F - 650 > > I want to add another column where all A,B should get an index 1, all C,Dan> index of 2 and all E,F an index of 3 so that the data.frame looks like: > > ID - Name - Value > 1 - A - 400 > 1 - A - 300 > 1 - B - 200 > 1 - B - 350 > 2 - C - 500 > 2 - C - 350 > 2 - D - 450 > 2 - D - 600 > 3 - E - 700 > 3 - E - 750 > 3 - F - 630 > 3 - F - 650 > > My data.frame is quite big so I cannot add all values by hand. > > > Cheers > > > > > -- > View this message in context:http://r.789695.n4.nabble.com/Data-alignment-tp4153024p4153024.html> Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help@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. >-- Sarah Goslee http://www.stringpage.com http://www.sarahgoslee.com http://www.functionaldiversity.org [[alternative HTML version deleted]]
try this:> match(x$Name> x <- read.table(text = "Name - Value+ A - 400 + A - 300 + B - 200 + B - 350 + C - 500 + C - 350 + D - 450 + D - 600 + E - 700 + E - 750 + F - 630 + F - 650", header = TRUE, as.is = TRUE)> map <- data.frame(key = c("A", "B", "C", "D", "E", "F")+ , value = c( 1, 1, 2, 2, 3, 3) + , stringsAsFactors = FALSE + )> x$ID <- map$value[match(x$Name, map$key)] > xName X. Value ID 1 A - 400 1 2 A - 300 1 3 B - 200 1 4 B - 350 1 5 C - 500 2 6 C - 350 2 7 D - 450 2 8 D - 600 2 9 E - 700 3 10 E - 750 3 11 F - 630 3 12 F - 650 3 On Sat, Dec 3, 2011 at 7:06 AM, Sarah Goslee <sarah.goslee at gmail.com> wrote:> I doubt your data frame looks like that, with all the -, but regardless you > can use ifelse() to construct your column. > > Sarah > > On Saturday, December 3, 2011, syrvn <mentor_ at gmx.net> wrote: >> Hello! >> >> I have a data.frame which looks like: >> >> Name - Value >> A - 400 >> A - 300 >> B - 200 >> B - 350 >> C - 500 >> C - 350 >> D - 450 >> D - 600 >> E - 700 >> E - 750 >> F - 630 >> F - 650 >> >> I want to add another column where all A,B should get an index 1, all C,D > an >> index of 2 and all E,F an index of 3 so that the data.frame looks like: >> >> ID - Name - Value >> 1 - A - 400 >> 1 - A - 300 >> 1 - B - 200 >> 1 - B - 350 >> 2 - C - 500 >> 2 - C - 350 >> 2 - D - 450 >> 2 - D - 600 >> 3 - E - 700 >> 3 - E - 750 >> 3 - F - 630 >> 3 - F - 650 >> >> My data.frame is quite big so I cannot add all values by hand. >> >> >> Cheers >> >> >> >> >> -- >> View this message in context: > http://r.789695.n4.nabble.com/Data-alignment-tp4153024p4153024.html >> Sent from the R help mailing list archive at Nabble.com. >> >> ______________________________________________ >> R-help at r-project.org 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. >> > > -- > Sarah Goslee > http://www.stringpage.com > http://www.sarahgoslee.com > http://www.functionaldiversity.org > > ? ? ? ?[[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org 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. >-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.
Thanks for your suggestions. I will try them. The "-" in my original post was actually only there to serve as a separator so that it is easier for you to see the data structure but apparently it rather confused you... sorry :) -- View this message in context: http://r.789695.n4.nabble.com/Data-alignment-tp4153024p4153112.html Sent from the R help mailing list archive at Nabble.com.
> > Re: [R] Data alignment > > Thanks for your suggestions. I will try them. > > The "-" in my original post was actually only there to serve as aseparator> so that it is easier for you to see the data structure but apparently it > rather confused you... sorry :)That is why dput is to be used. Try this by yourself> dput(y)structure(c(2L, 1L, 1L, NA, 2L, 1L, NA, NA, 2L, NA, NA, NA, NA, 1L, NA, NA, NA, 2L, NA, 1L, NA, 2L, NA, 3L, NA), .Dim = c(5L, 5L), .Dimnames = list(NULL, c("", "BMW", "Mercedes", "VW", "Skoda" ))) x<-structure(c(2L, 1L, 1L, NA, 2L, 1L, NA, NA, 2L, NA, NA, NA, NA, 1L, NA, NA, NA, 2L, NA, 1L, NA, 2L, NA, 3L, NA), .Dim = c(5L, 5L), .Dimnames = list(NULL, c("", "BMW", "Mercedes", "VW", "Skoda" )))> all.equal(x,y)[1] TRUE>Regards Petr> > -- > View this message in context: http://r.789695.n4.nabble.com/Data- > alignment-tp4153024p4153112.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.