Re: [R] From FAQ 7.21 to a command like apply(sapply(list(f1, f2, f3), is.na), 2, sum)

From: Thomas Lumley <tlumley_at_u.washington.edu>
Date: Wed 30 Mar 2005 - 10:13:18 EST

On Wed, 30 Mar 2005, Heinz Tuechler wrote:

> Dear all,
>
> Last December there was a thread regarding the famous FAQ 7.21 "How can I
> turn a string into a variable?" and asking what people want to do with
> these strings.
> My, certainly trivial application would be as follows:
> Assume I have a data.frame containing besides others also the columns f1,
> f2, ..., fn and I want to create a command like:
> apply(sapply(list(f1,f2,f3),is.na),2,sum)
> or
> summary(cbind(f1,f2,f3))
>
> Can I start from paste('f',1:3,sep='') to arrive at the abovementioned
> command?

No parse,as.name or other complications needed. It's all just indexing. Suppose your data frame is called dd

fs<-paste('f',1:3,sep='')
apply(sapply(dd[,fs],is.na),2,sum)
summary(dd[,fs])

         -thomas



R-help@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 Received on Wed Mar 30 10:25:15 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:30:57 EST