[R] how to update a value in a list with lapply

ce zadig_1 at excite.com
Sun May 10 01:35:13 CEST 2015


Dear All,

I have a list, using lapply I find some elements of the list, and then I want to change the values I find. but it doesn't work:

 foo<-list(A = c(1,3), B =c(1, 2), C = c(3, 1))
 lapply(foo, function(x) if(x[1] == 1 ) x )
$A
[1] 1 3

$B
[1] 1 2

$C
NULL

 lapply(foo, function(x) if(x[1] == 1 ) x[2] <- 0 )
$A
[1] 0

$B
[1] 0

$C
NULL

>  lapply(foo, function(x) if(x[1] == 1 ) x )
$A
[1] 1 3

$B
[1] 1 2

$C
NULL


how to do it correctly ?
thanks



More information about the R-help mailing list