[R] Sprintf to call data frame from environment
Jim Lemon
drjimlemon at gmail.com
Wed May 25 00:39:17 CEST 2016
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.
More information about the R-help
mailing list