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?
I tried get, parse, as.name, eval in diverse combinations but did not reach
a solution.
More generally my question is, how can I produce a "list" of variables like
x1 to xn in a convenient way within a command.
I am quite sure that this has been answered several times, but I did not
find one of these answers. So I welcome any hint, where to look.
Heinz T?chler
From FAQ 7.21 to a command like apply(sapply(list(f1,f2,f3),is.na),2,sum)
4 messages · Thomas Lumley, Heinz Tuechler
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
At 16:13 29.03.2005 -0800, Thomas Lumley wrote:
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
Thank you, Thomas, for your answer. I was curious if there was a simple way to do this without referring to the data.frame, so that the resulting command would correspond in its effect exactly to the abovementioned examples. It's not urgent, but I will try further. Many thanks Heinz
At 09:32 30.03.2005 +0200, Heinz Tuechler wrote:
At 16:13 29.03.2005 -0800, Thomas Lumley wrote:
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
Thank you, Thomas, for your answer. I was curious if there was a simple way to do this without referring to the data.frame, so that the resulting command would correspond in its effect exactly to the abovementioned
examples.
It's not urgent, but I will try further. Many thanks Heinz
Continuation:
Maybe not an elegant solution, but it seems to work:
apply(sapply(eval(parse(text=paste('list(',paste('f',1:3,sep='',
collapse=','),')'))) ,is.na),2,sum)
What I missed in my earlier attempts was "collapse=','".
Heinz