[R] Problem with modifying a data frame

Leon Yee yee.leon at gmail.com
Wed Aug 12 06:57:46 CEST 2009


Hi,

> this could be a simple question but I am looking into modifying a data frame
> using a "condition" without the need to loop over that data, would that be
> possible?
> 
> I have tried the following 
> 
>> x<-c(4,5,6,6,8)
>> y<-c("a","b","b","b","c")
>> data<-data.frame(x,y)
>> data
>   x y
> 1 4 a
> 2 5 b
> 3 6 b
> 4 6 b
> 5 8 c
>  if (data$x==6){
> data$x<-66
> }
> Warning message:
> In if (data$x == 6) { :
>   the condition has length > 1 and only the first element will be used

data[which(data$x == 6), 'x'] <- 66
should be what you want.

> I woul dlike to change the 6 in column x into 66
> why the above did not work?

Best,
Leon




More information about the R-help mailing list