[R] Logical operator and lists

arun smartpink111 at yahoo.com
Tue Jan 8 19:31:40 CET 2013

```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.

________________________________
From: Dominic Roye <dominic.roye at gmail.com>
To: arun <smartpink111 at yahoo.com>
Sent: Tuesday, January 8, 2013 1:06 PM
Subject: Re: [R] Logical operator and lists

hi,

Can you explain me why without ;x at the end, i get only NA?

> c
[[1]]
[1] NA

[[2]]
[1] NA

2013/1/8 arun <smartpink111 at yahoo.com>

HI,
>This should also work:
>
>
> 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==""]<-NA;x})
>
>A.K.
>
>----- Original Message -----
>From: Dominic Roye <dominic.roye at gmail.com>
>To: R help <r-help at r-project.org>
>Cc:
>Sent: Tuesday, January 8, 2013 7:16 AM
>Subject: [R] Logical operator and lists
>
>
>Hello R-Helpers,
>
>I have a slight problem with the expresion data[data==""] <- NA which works
>well for a data.frame. But now i must use the same for a list of
>data.frames.
>
>My idea is data[[]][data==""] but it don´t work.
>
>Thanks!!
>
>Dominic
>
>
>    [[alternative HTML version deleted]]
>
>
>______________________________________________
>R-help at r-project.org mailing list
>https://stat.ethz.ch/mailman/listinfo/r-help