Minor correction, given your code, values less than 3 will be coded as "S" since they are less than 15.23. In the code I suggested, values less than 3 will be coded as missing (NA).
David C -----Original Message----- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of David L Carlson Sent: Friday, October 17, 2014 9:15 AM To: Monaly Mistry; r-help@r-project.org Subject: Re: [R] assigning letter to a column I think it is doing exactly what you have told it to do, but that is probably not what you want it to do. First, you do not need a loop since the ifelse() function is vectorized. Read the manual page and the examples carefully. Also you are coding ifelse() as if it were the same as if() {} else() {}. Again you need to refer to the documentation. Second, this seems like a job for cut() not ifelse(). Third, look at your code. The first statement is x$COR_LOC>=3 | x$COR_LOC<15.230 so everything greater than 3 will be coded as "S." That is probably all of your data. You probably want to use & (and) instead of | (or). It is not clear what you want to happen for values less than 3 but they will be NA (missing). Your entire ifelse() boils down to set.seed(42) x <- data.frame(COR_LOC=runif(100, 0, 30)) x$ForS <- cut(x$COR_LOC, breaks=c(3, 15.23, 19.81, 25.40, Inf), labels=c("S", "I1", "I2", "F"), right=FALSE) No loops, no ifelse's. Anything below 3 will ------------------------------------- David L Carlson Department of Anthropology Texas A&M University College Station, TX 77840-4352 -----Original Message----- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Monaly Mistry Sent: Friday, October 17, 2014 8:27 AM To: r-help@r-project.org Subject: [R] assigning letter to a column Hi, I'm having trouble with assigning a letter to a column based on the value of another column. Since I have separate data files I've saved then into one folder and I'm reading them in separately into the function. The code is below. #F= fast; S= slow; I1= Intermediate score 1; I2=Intermediate score 2 filename<-list.files(pattern="*.txt") filename corloc<- function(x){ x<-read.table(filename[x], sep="\t", header=TRUE) #will extract the relevant data file from folder 1998. ex. corloc(1) will return 1998 breeding year data x[,"ForS"]<-0 #new column for (i in length(x$CORLOC)){ #this is the bit that I'm having a problem with since it's not assigning the appropriate letter into the "ForS" column ifelse(x$COR_LOC>=3 | x$COR_LOC<15.230, ForS<-"S", ifelse(x$COR_LOC>=15.230 | x$COR_LOC<19.810, ForS<-"I1", ifelse(x$COR_LOC>=19.810 | x$COR_LOC<25.540, FS<-"I2",ForS<-"F")))} print(x) } I've tried some of the solutions on stackoverflow but still was unsuccessful. Best, Monaly. [[alternative HTML version deleted]] ______________________________________________ R-help@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. ______________________________________________ R-help@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. ______________________________________________ R-help@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.