[R] setting a number of values to NA over a data.frame.
Erik Iverson
iverson at biostat.wisc.edu
Wed Feb 7 23:57:40 CET 2007
John -
Your initial problem uses 0, but the example uses 1 for the value that
gets an NA. My solution uses 1 to fit with your example. There may be
a better way, but try something like
data1[3:5] <- data.frame(lapply(data1[3:5], function(x) ifelse(x==1, NA,
x)))
The data1[3:5] is just a test subset of columns I chose from your data1
example. Notice it appears twice, once on each side of the assignment
operator.
In English, apply to each column of the data frame (which is a list) a
function that will return NA if the element is 1, and the value
otherwise, and then turn the modified lists into a data.frame, and save
it as data1.
See the help files for lapply and ifelse if you haven't seen those before.
Maybe someone has a better way?
Erik
John Kane wrote:
> This is probably a simple problem but I don't see a
> solution.
>
> I have a data.frame with a number of columns where I
> would like 0 <- NA
>
> thus I have df1[,144:157] <- NA if df1[, 144: 157] ==0
> and df1[, 190:198] <- NA if df1[, 190:198] ==0
>
> but I cannot figure out a way do this.
>
> cata <- c( 1,1,6,1,1,NA)
> catb <- c( 1,2,3,4,5,6)
> doga <- c(3,5,3,6,4, 0)
> dogb <- c(2,4,6,8,10, 12)
> rata <- c (NA, 9, 9, 8, 9, 8)
> ratb <- c( 1,2,3,4,5,6)
> bata <- c( 12, 42,NA, 45, 32, 54)
> batb <- c( 13, 15, 17,19,21,23)
> id <- c('a', 'b', 'b', 'c', 'a', 'b')
> site <- c(1,1,4,4,1,4)
> mat1 <- cbind(cata, catb, doga, dogb, rata, ratb,
> bata, batb)
>
> data1 <- data.frame(site, id, mat1)
> data1
>
> # Obviously this works fine for one column
>
> data1$site[data1$site ==1] <- NA ; data1
>
> but I cannot see how to do this with indices that
> would allow me to do more than one column in the
> data.frame.
>
> At one point I even tried something like this
> a <- c("site")
> data1$a[data1$a ==1] <- NA
>
> which seems to produce a corrupt data.frame.
>
> I am sure it is simple but I don't see it.
>
> Any help would be much appreciated.
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
More information about the R-help
mailing list