[R] assigning letter to a column

Monaly Mistry monaly.mistry at gmail.com
Fri Oct 17 15:26:54 CEST 2014


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



More information about the R-help mailing list