[R] Replace values in a dataframe

Grams Robins grams_robins at yahoo.com
Wed Jun 17 19:47:16 CEST 2015


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]]



More information about the R-help mailing list