Skip to content
Prev 314859 / 398506 Next

Logical operator and lists

Hi,

If you don't return(x) or x at the end,
?set.seed(5)
?list1<-lapply(1:3,function(i) data.frame(col1=sample(c(1:5,""),10,replace=TRUE), value=rnorm(10),stringsAsFactors=FALSE))
?lapply(list1,function(x) x[x==""])
#[[1]]
#[1] "" ""

#[[2]]
#character(0)

#[[3]]
#[1] "" "" "" "" "" ""


?lapply(list1,function(x) x[x==""]<-NA)
#[[1]]
#[1] NA
#
#[[2]]
#[1] NA
#
#[[3]]
#[1] NA
?lapply(list1,function(x) x[x==""]<-rep(NA,length(x[x==""])))
#[[1]]
#[1] NA NA
#
#[[2]]
#logical(0)
#
#[[3]]
#[1] NA NA NA NA NA NA
?lapply(list1,function(x) {x[x==""]<-NA;return(x)})
#or
lapply(list1,function(x) {x[x==""]<-NA;x})
#or
?lapply(list1,function(x) {x[x==""]<-rep(NA,length(x[x==""]));x})
[[1]]
#?? col1????? value
#1???? 2 -0.6029080
#2???? 5 -0.4721664
#3? <NA> -0.6353713
#4???? 2 -0.2857736
#5???? 1? 0.1381082
#6???? 5? 1.2276303
#7???? 4 -0.8017795
#8???? 5 -1.0803926
#9? <NA> -0.1575344
#10??? 1 -1.0717600
----------------------

A.K.
Message-ID: <1357669900.96323.YahooMailNeo@web142603.mail.bf1.yahoo.com>
In-Reply-To: <CALvVS-EQW=Tvw4yxyCDVpaQFWDOkF4zVB7SAY+NMgNOsAcpPbw@mail.gmail.com>