In my environment I have a data frame called Samples_1.txt.
From this data frame I need to get variable V1. My code doesn't work.
Note: I need to do it in this way because I have the code into a for loop.
sprintf("Samples_%s.txt", 1)$V1
Sprintf to call data frame from environment
5 messages · Beatriz González Domínguez, Nordlund, Dan (DSHS/RDA), David Winsemius +1 more
In my environment I have a data frame called Samples_1.txt.
From this data frame I need to get variable V1. My code doesn't work. Thanks!
sprintf("Samples_%s.txt", 1)$V1
Note: I need to do it in this way because I have the code into a for loop.
It is not clear (at least to me) what your actual task is. But, if Samples_1.txt is the actual name of a data frame that exists in memory (and not a filename), then you need to wrap the sprintf() in a get() function.
get(sprintf("Samples_%s.txt", 1))$V1
I am no expert on "computing on the language" in R, but I can't help but think you are going about your task in the wrong way. If you provide more detail about what you are trying to do, someone will probably be able to provide you a solution where you don't need to do it this way.
Hope this is helpful,
Dan
Daniel Nordlund, PhD
Research and Data Analysis Division
Services & Enterprise Support Administration
Washington State Department of Social and Health Services
-----Original Message-----
From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Beatriz
Sent: Tuesday, May 24, 2016 2:01 PM
To: r-help at r-project.org
Subject: [R] Sprintf to call data frame from environment
In my environment I have a data frame called Samples_1.txt.
From this data frame I need to get variable V1. My code doesn't work.
Thanks!
sprintf("Samples_%s.txt", 1)$V1
Note: I need to do it in this way because I have the code into a for loop.
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting- guide.html and provide commented, minimal, self-contained, reproducible code.
On May 24, 2016, at 2:01 PM, Beatriz <aguitatierra at hotmail.com> wrote: In my environment I have a data frame called Samples_1.txt. From this data frame I need to get variable V1. My code doesn't work. Thanks! $V1 Note: I need to do it in this way because I have the code into a for loop.
You are treating this as if R were a macro processor, which it's not. The only function that lets you pull in a data-object from the store of named objects using a character vector is `get`, so perhaps:
get( sprintf("Samples_%s.txt", 1) )$V1
And if you were considering the next step of hoping to pass a computed item to `$`, then forget that as well, and learn to use `[`.
David Winsemius Alameda, CA, USA
Hi Beatriz,
I'll guess that you have a number of files with names like this:
Samples_1.txt
Samples_2.txt
...
Each one can be read with a function like read.table and will return a
data frame with default names (V1, V2, ...). You then want to extract
the first element (column) of the data frame. If I'm correct, try
this:
V1s<-list()
# nfiles is the number of files you want to read
for(i in 1:nfiles) {
filename<-paste("Samples_",i,".txt,sep="")
# you will probably have to add the appropriate arguments to read.table
V1s[[i]]<-read.table(filename)$V1
}
The list V1s should contain the first columns of all the data frames read.
Jim
On Wed, May 25, 2016 at 7:01 AM, Beatriz <aguitatierra at hotmail.com> wrote:
In my environment I have a data frame called Samples_1.txt.
From this data frame I need to get variable V1. My code doesn't work.
Thanks!
sprintf("Samples_%s.txt", 1)$V1
Note: I need to do it in this way because I have the code into a for loop.
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.