[R] Substitute NAs in a data frame
Sharpie
chuck at sharpsteen.net
Thu Mar 18 22:15:42 CET 2010
Marsh wrote:
>
> 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
>
You had it correct if you use lapply, you just need to name the additional
arguments you are passing:
myList <- lapply( myList, sub, pattern = '--', replacement = NA )
The sub-lists of myList will be positionally matched to the third argument
of the sub function, which is x.
Hope this helps!
-Charlie
-----
Charlie Sharpsteen
Undergraduate-- Environmental Resources Engineering
Humboldt State University
--
View this message in context: http://n4.nabble.com/Substitute-NAs-in-a-data-frame-tp1598621p1598638.html
Sent from the R help mailing list archive at Nabble.com.
More information about the R-help
mailing list