Hey all, I have a dataframe that consists of: structure(list(Color = c("5", "<4","5", "<5", "5"), Unit = c("Hazen", "Hazen", "Hazen", "Hazen", "Hazen")), .Names = c("Color", "Unit"), row.names c("1:2", "1:3", "1:4", "1:5","1:6"), class = "data.frame") I need to find the <4 and have a new column with the result of 4 ? 2 = 2 Similarly I need to find the <5 and have the new column with the result of 5 ? 2 = 2.5 All other numeric values would be added to the new column also to end up with: Color New value Unit 1:2 5 5 Hazen 1:3 <4 2 Hazen 1:4 5 5 Hazen 1:5 <5 2.5 Hazen 1:6 5 5 Hazen Thanks for your help!! -- Shane [[alternative HTML version deleted]]
Hi Shane, On Wed, Jun 17, 2015 at 1:31 PM, Shane Carey <careyshan at gmail.com> wrote:> Hey all, > > I have a dataframe that consists of: > > structure(list(Color = c("5", "<4","5", "<5", "5"), Unit = c("Hazen", > "Hazen", > "Hazen", "Hazen", "Hazen")), .Names = c("Color", "Unit"), row.names > c("1:2", > "1:3", "1:4", "1:5","1:6"), class = "data.frame")Thanks for providing data.> > I need to find the <4 and have a new column with the result of 4 ? 2 = 2 > > Similarly > > I need to find the <5 and have the new column with the result of 5 ? 2 = 2.5Are "<4" and "<5" the only possible non-numeric values? If so, this is an easy way to do it:> mydata <- structure(list(Color = c("5", "<4","5", "<5", "5"), Unit = c("Hazen",+ "Hazen", + "Hazen", "Hazen", "Hazen")), .Names = c("Color", "Unit"), row.names + c("1:2", + "1:3", "1:4", "1:5","1:6"), class = "data.frame")> mydataColor Unit 1:2 5 Hazen 1:3 <4 Hazen 1:4 5 Hazen 1:5 <5 Hazen 1:6 5 Hazen> mydata$NewColor <- ifelse(mydata$Color == "<4", 4/2, ifelse(mydata$Color == "<5", 5/2, as.numeric(mydata$Color))) > mydataColor Unit NewColor 1:2 5 Hazen 5.0 1:3 <4 Hazen 2.0 1:4 5 Hazen 5.0 1:5 <5 Hazen 2.5 1:6 5 Hazen 5.0 This will throw a warning message that you can safely ignore.> All other numeric values would be added to the new column also to end up > with: > > Color New value Unit 1:2 5 5 Hazen 1:3 <4 2 Hazen 1:4 5 5 Hazen 1:5 <5 > 2.5 Hazen 1:6 5 5 HazenBonus points for providing data, demerits for posting in HTML so your email got mangled. Sarah -- Sarah Goslee http://www.functionaldiversity.org
Try this:? dat=structure(list(Color = c("5", "<4","5", "<5", "5"), Unit = c("Hazen","Hazen","Hazen", "Hazen", "Hazen")), .Names = c("Color", "Unit"), row.names =c("1:2","1:3", "1:4", "1:5","1:6"), class = "data.frame") dat=as.data.frame(dat)dat$col2 <- rep(" ", nrow(dat))dat[dat$Color == "<4", ][, "col2"] <- "2"dat[dat$Color == "<5", ][, "col2"] <- "2.5" On Wednesday, June 17, 2015 1:33 PM, Shane Carey <careyshan at gmail.com> wrote: Hey all, I have a dataframe that consists of: structure(list(Color = c("5", "<4","5", "<5", "5"), Unit = c("Hazen", "Hazen", "Hazen", "Hazen", "Hazen")), .Names = c("Color", "Unit"), row.names c("1:2", "1:3", "1:4", "1:5","1:6"), class = "data.frame") I need to find the <4 and have a new column with the result of 4 ? 2 = 2 Similarly I need to find the <5 and have the new column with the result of 5 ? 2 = 2.5 All other numeric values would be added to the new column also to end up with: ? Color New value Unit 1:2 5 5 Hazen 1:3 <4 2 Hazen 1:4 5 5 Hazen 1:5 <5 2.5 Hazen 1:6 5 5 Hazen Thanks for your help!! -- Shane ??? [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see 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. [[alternative HTML version deleted]]
Is the following what you want: (z is your data frame)> change <-c("2","2.5") > names(change) <- c("<4","<5")(note: this can be automated using regular expressions and will work for lots more values to change. Sarah's ifelse() solution is fine for the example, but becomes too cumbersome (as she intimated) for more values.)> z$newcol <- with(z,{+ for(nm in names(change))Color[Color == nm]<-change[nm] + as.numeric(Color) + })> zColor Unit newcol 1:2 5 Hazen 5.0 1:3 <4 Hazen 2.0 1:4 5 Hazen 5.0 1:5 <5 Hazen 2.5 1:6 5 Hazen 5.0 Incidentally, if this is how you are attempting to deal with censored data, it may be a very bad idea. For why, consult a local statistician or post on a statistics list like stats.stackexchange.com Cheers, Bert Bert Gunter "Data is not information. Information is not knowledge. And knowledge is certainly not wisdom." -- Clifford Stoll On Wed, Jun 17, 2015 at 10:31 AM, Shane Carey <careyshan at gmail.com> wrote:> Hey all, > > I have a dataframe that consists of: > > structure(list(Color = c("5", "<4","5", "<5", "5"), Unit = c("Hazen", > "Hazen", > "Hazen", "Hazen", "Hazen")), .Names = c("Color", "Unit"), row.names > c("1:2", > "1:3", "1:4", "1:5","1:6"), class = "data.frame") > > I need to find the <4 and have a new column with the result of 4 ? 2 = 2 > > Similarly > > I need to find the <5 and have the new column with the result of 5 ? 2 > 2.5 > > > All other numeric values would be added to the new column also to end up > with: > > > > Color New value Unit 1:2 5 5 Hazen 1:3 <4 2 Hazen 1:4 5 5 Hazen 1:5 <5 > 2.5 Hazen 1:6 5 5 Hazen > > > > Thanks for your help!! > > -- > Shane > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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.[[alternative HTML version deleted]]