I have a data frame somewhat like this: myframe <- data.frame (ID=c(2,3,4,5), Hunger =c(415,452,550,318 )) myframe Now I would like to add a column to the right which summarizes the values for Hunger somewhat to reduce the number of values: If the values for Hunger are between 300-400 I would like to insert the number 350, between 400-500 insert 450 between 500-600 insert 550 Does anyone know how? Cause I don't and my brain already hurts. Can't be that difficult, right? -- View this message in context: http://r.789695.n4.nabble.com/if-between-500-600-give-550-tp4649044.html Sent from the R help mailing list archive at Nabble.com.
Hello, Try myframe$new <- NA myframe$new[300 <= myframe$Hunger & myframe$Hunger < 400] <- 350 myframe$new[400 <= myframe$Hunger & myframe$Hunger < 500] <- 450 myframe$new[500 <= myframe$Hunger & myframe$Hunger < 600] <- 550 myframe Why reduce the number of values? Hope this helps, Rui Barradas Em 09-11-2012 13:10, Tagmarie escreveu:> I have a data frame somewhat like this: > > myframe <- data.frame (ID=c(2,3,4,5), Hunger =c(415,452,550,318 )) > myframe > > Now I would like to add a column to the right which summarizes the values > for Hunger somewhat to reduce the number of values: If the values for Hunger > are between > 300-400 I would like to insert the number 350, > between > 400-500 insert 450 > between > 500-600 insert 550 > > Does anyone know how? Cause I don't and my brain already hurts. Can't be > that difficult, right? > > > > > -- > View this message in context: http://r.789695.n4.nabble.com/if-between-500-600-give-550-tp4649044.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.
Hi, Try this: myframe$newcol<-ifelse(myframe[,2]>=300 & myframe[,2]<400,350,ifelse(myframe[,2]>=400 &myframe[,2]<500,450,ifelse(myframe[,2]>=500 & myframe[,2]<600,550,NA))) ?myframe #? ID Hunger newcol #1? 2??? 415??? 450 #2? 3??? 452??? 450 #3? 4??? 550??? 550 #4? 5??? 318??? 350 A.K. ----- Original Message ----- From: Tagmarie <Ramgad82 at gmx.net> To: r-help at r-project.org Cc: Sent: Friday, November 9, 2012 8:10 AM Subject: [R] if between 500-600 give 550 I have a data frame somewhat like this: myframe <- data.frame (ID=c(2,3,4,5), Hunger =c(415,452,550,318 )) myframe Now I would like to add a column to the right which summarizes the values for Hunger somewhat to reduce the number of values: If the values for Hunger are between 300-400 I would like to insert the number 350, between 400-500 insert 450 between 500-600 insert 550 Does anyone know how? Cause I don't and my brain already hurts. Can't be that difficult, right? -- View this message in context: http://r.789695.n4.nabble.com/if-between-500-600-give-550-tp4649044.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.
On Nov 9, 2012, at 5:10 AM, Tagmarie wrote:> I have a data frame somewhat like this: > > myframe <- data.frame (ID=c(2,3,4,5), Hunger =c(415,452,550,318 )) > myframe > > Now I would like to add a column to the right which summarizes the values > for Hunger somewhat to reduce the number of values: If the values for Hunger > are between > 300-400 I would like to insert the number 350, > between > 400-500 insert 450 > between > 500-600 insert 550 > > Does anyone know how? Cause I don't and my brain already hurts. Can't be > that difficult, right?> myframe$grpH <- c(350, 450, 550)[findInterval(myframe$Hunger, c(300, 400, 500, 600) ) ]> myframeID Hunger grpH 1 2 415 450 2 3 452 450 3 4 550 550 4 5 318 350 Please note that your specification had overlapping intervals and that 'findInterval' by default uses closed intervals on the left and open intervals on the right. (This is the opposite of the default behavior of cut(). ) So I suppose you could say R's implementations are just as ambiguous as your problem specification.> myframe$grpHc <- cut(myframe$Hunger, breaks=c(300, 400, 500, 600), labels=c("350", "450", "550") ) > myframeID Hunger grpH grpHc 1 2 415 450 450 2 3 452 450 450 3 4 550 550 550 4 5 318 350 350 Note also that cut returns a factor:> lapply(myframe, class)$ID [1] "numeric" $Hunger [1] "numeric" $grpH [1] "numeric" $grpHc [1] "factor" -- David Winsemius, MD Alameda, CA, USA