s-news
[Top] [All Lists]

[S] S-Plus Question

To: s-news <s-news@wubios.wustl.edu>
Subject: [S] S-Plus Question
From: "paul.na" <paul.na@bankofamerica.com>
Date: Thu, 16 Dec 1999 11:38:32 -0500
Sender: owner-s-news@wubios.wustl.edu
Here is a summary of replies to my original posting.
Thank you very much to those who replied (their names are given below).

Matt Kurbat

ORIGINAL POSTING
> Dear Readers,
>
> I'd like to be able to do the following in Splus.
>
> Say you have a dataframe called DF,
> and variables V1 through Vn in this dataframe.
>
> Say also you have a string "temp" such that
> temp is "DF$V1".  For example you might have defined temp as
>
>         temp<-paste("DF$","V1",sep="")
>
> I'd like to be able to use this character string "temp" to access
> the variable V1 in dataframe DF - for example, to be able to
> assign DF$V1 to a new varible.  What do I do to "temp" to
> be able to access the variable V1 in dataframe DF?
>
> Thank you,
> Matt Kurbat

REPLIES:
Thank you very much to all the following for their answers:
Brad Biggerstaff
Bill Dunlap
Nick Ellis
Andrzej Galecki
Bert Gunter
Tim Hesterberg
Albyn Jones
Renaud Lancelot
Nick Locantore
Andrew Sinclair
David Smith
Bill Venables

There were several different types of answers.  I've extracted one example
of each type (or in some cases combined answers).  Results are presented
below.

----------------------------------------------------------------------------
----------------------------------

I think you want

*newvble <- eval(parse(text=temp))
*(*OR: eval(parse(text="DF$V1"))**)
*(*OR: eval(parse(text=paste("DF$","V1",sep="")))**)

The parse() converts your string into an 'expression'.
eval() then evaluates the expression.  You may need to
eval(, frame = sys.parent()) or something else cleverer
if this is all done inside a function.
----------------------------------------------------------------------------
----------------------------------

get(temp)

or

get("DF$V1")

or if you want to assign all Vi at the same time:

for(i = 1:n) assign(paste("new.var", i, sep=""), get(paste("DF$", "V",
i, sep="")))

(if you're interested in the last solution, beware of the arguments
"immediate", "where" and "frame")

----------------------------------------------------------------------------
----------------------------------

I think that this is what you want:

> attach(fuel.frame)
> names(fuel.frame)
[1] "Weight"  "Disp."   "Mileage" "Fuel"    "Type"
> temp<-paste("fuel.frame$","Type",sep="")
> newvar <- eval(parse(text=temp)
----------------------------------------------------------------------------
----------------------------------


I'm not sure how to do exactly what you're asking, but maybe this will help.
You can still access the columns of DF using the names "V1" ... "Vn" by
using do.call() togther with the subscripting functions.  Variations on the
function ("[","[[","$") and the column-list argument are useful for various
things; note column numbers are accepted, too (see last example).

For example:
> DF<-data.frame(V1=1:10,V2=11:20,V3=letters[rep(1:2,5)])
> DF
   V1 V2 V3
 1  1 11  a
 2  2 12  b
 3  3 13  a
 4  4 14  b
 5  5 15  a
 6  6 16  b
 7  7 17  a
 8  8 18  b
 9  9 19  a
10 10 20  b
>
> do.call("[",list(DF,"V2"))
   V2
 1 11
 2 12
 3 13
 4 14
 5 15
 6 16
 7 17
 8 18
 9 19
10 20
> do.call("[[",list(DF,"V2"))
 [1] 11 12 13 14 15 16 17 18 19 20
>
> do.call("$",list(DF,"V2"))
 [1] 11 12 13 14 15 16 17 18 19 20
>
> do.call("[",list(DF,c("V1","V3")))
   V1 V3
 1  1  a
 2  2  b
 3  3  a
 4  4  b
 5  5  a
 6  6  b
 7  7  a
 8  8  b
 9  9  a
10 10  b
>
> do.call("[",list(DF,paste("V",c(1,3),sep="")))
   V1 V3
 1  1  a
 2  2  b
 3  3  a
 4  4  b
 5  5  a
 6  6  b
 7  7  a
 8  8  b
 9  9  a
10 10  b
>
> do.call("[",list(DF,c(1,3)))
   V1 V3
 1  1  a
 2  2  b
 3  3  a
 4  4  b
 5  5  a
 6  6  b
 7  7  a
 8  8  b
 9  9  a
10 10  b

Additional to my previous note:

You could unpaste temp at $ and use "V1" using one of the methods I sent
previously (though suggesting the paste wasn't needed):
unpaste(temp,sep="$")[[2]] will get you "V1" directly.  You have to be
careful when applying unpaste() willy-nilly, as it will work for namings of
list of lists, but the 2 might then be wrong:

> temp2<-"DF$L1$V1"
> unpaste(temp2,sep="$")
[[1]]:
[1] "DF"

[[2]]:
[1] "L1"

[[3]]:
[1] "V1"
----------------------------------------------------------------------------
----------------------------------

simpler is to use constructs like df[,"V1"]

temp<-paste("V",1,sep="")
newvar <- df[,temp]

why not just use the natural subsetting syntax: X[,temp],
leaving off the "DF$" prefix:

> X <- data.frame(a1=rnorm(10),a2=rnorm(10),a3=rnorm(10))
> X
             a1          a2          a3
 1 -0.009327616 -0.63203923 -1.10863723
 2 -0.883845067  1.88446473  0.26173191
 3 -0.899785204  0.82985563  0.86992442
 4  1.966962166  0.91691809 -1.45683723
 5  1.148358581  1.12844552 -0.45587880
 6  0.446811886 -0.99774637 -0.05244959
 7 -1.457353823  0.03401917 -0.53959360
 8  0.292276032 -0.42210101  0.15247698
 9  0.538080257 -0.76695623 -0.74940718
10 -0.255957675  0.42023236  0.28882342
> n <- c("a1","a2")
> X[,n]
             a1          a2
 1 -0.009327616 -0.63203923
 2 -0.883845067  1.88446473
 3 -0.899785204  0.82985563
 4  1.966962166  0.91691809
 5  1.148358581  1.12844552
 6  0.446811886 -0.99774637
 7 -1.457353823  0.03401917
 8  0.292276032 -0.42210101
 9  0.538080257 -0.76695623
10 -0.255957675  0.42023236
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu.  To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message:  unsubscribe s-news

<Prev in Thread] Current Thread [Next in Thread>