[R] Substitute NAs in a data frame

Marshall Feldman marsh at uri.edu
Thu Mar 18 22:01:21 CET 2010


Excuse me for what I'm sure is a stupid beginner's question, but I've 
given up trying to find the answer to this question from the help, 
RSiteSearch, or any of the usual places.

I have a list that looks like this:
 >myList
$first
[1] "--" "18" "8" "32"

$second
[1] "--" "--" "40" "54"

I want a straightforward way to replace "--" with NA so that the list 
looks like:

 >myList
$first
[1] NA "18" "8" "32"

$second
[1] NA NA "40" "54"

Now I know I can do something like:

     myList$first <- sub("--",NA,myList$first)

but the real list has lots of components. So is there some easy way to 
do something like:

     myList <- applier(myList,sub,"--",NA)

where "applier" is a function that will do what I want? I tried using 
lapply, sapply, etc. without luck.

     Thank,
     Marsh



More information about the R-help mailing list