Hi there, I am trying to import 100 files with the names of "vpn 1 .dat" to "vpn 100 .dat" into a respective table calld vpn1 to vpn100. I therfore have created a variable X<-1:100 I not want to use X as a subtitute for the number in my filename, so that I have to write only one function and it does the operation with all files. I have tried every combination i could imagine to include the string X into the file name: vpn'X' , vpn"X" , vpn[X] , and so on, but R never did what I wanted it too. vpn"X"<-read.table("vpn "X" .dat") So is there a way to do this in R??? or should I use an intirely new aproach? Thanks for your Help! -- View this message in context: http://r.789695.n4.nabble.com/Using-a-sting-in-variable-names-tp3527318p3527318.html Sent from the R help mailing list archive at Nabble.com.
On 16/05/2011 4:18 PM, olafgy wrote:> Hi there, > > I am trying to import 100 files with the names of "vpn 1 .dat" to "vpn 100 > .dat" > into a respective table calld vpn1 to vpn100. > > I therfore have created a variable X<-1:100 > > I not want to use X as a subtitute for the number in my filename, so that I > have to write only one function and it does the operation with all files. > > I have tried every combination i could imagine to include the string X into > the file name: > > vpn'X' , vpn"X" , vpn[X] , and so on, but R never did what I wanted it > too. > > vpn"X"<-read.table("vpn "X" .dat") > > So is there a way to do this in R??? or should I use an intirely new > aproach?Yes there is a way, and yes you should use a different approach. R is not a macro language. You can't just put X somewhere and expect it to be expanded into its contents. You need to call functions that make use of it and produce the results you want. One way is like this: filenames <- sprintf("vpn %d .dat", X) varnames <- sprintf("vpn%d", X) for (i in 1:100) { var <- read.table(filenames[i]) assign(varnames[i], var) } There are ways to compress all of this into a single line, but they are (in my opinion) a lot less clear. Duncan Murdoch
On Mon, 2011-05-16 at 13:18 -0700, olafgy wrote:> Hi there, > > I am trying to import 100 files with the names of "vpn 1 .dat" to "vpn 100 > .dat" > into a respective table calld vpn1 to vpn100. > > I therfore have created a variable X<-1:100 > > I not want to use X as a subtitute for the number in my filename, so that I > have to write only one function and it does the operation with all files. > > I have tried every combination i could imagine to include the string X into > the file name: > > vpn'X' , vpn"X" , vpn[X] , and so on, but R never did what I wanted it > too. > > vpn"X"<-read.table("vpn "X" .dat") > > So is there a way to do this in R??? or should I use an intirely new > aproach? > > Thanks for your Help!How about paste() to form the strings and assign() to save your objects?> for(i in 1:100) assign(paste("vpn", i, sep=""),read.table(paste("vpn", i, ".dat"))) HTH, Jerome [[alternative HTML version deleted]]
olafgy <olafgy <at> gmail.com> writes:> > Hi there, > > I am trying to import 100 files with the names of "vpn 1 .dat" to "vpn 100 > .dat" > into a respective table calld vpn1 to vpn100. > > I therfore have created a variable X<-1:100 > > I not want to use X as a subtitute for the number in my filename, so that I > have to write only one function and it does the operation with all files. > > I have tried every combination i could imagine to include the string X into > the file name: > > vpn'X' , vpn"X" , vpn[X] , and so on, but R never did what I wanted it > too. > > vpn"X"<-read.table("vpn "X" .dat") > > So is there a way to do this in R??? or should I use an intirely new > aproach? > > Thanks for your Help! > > -- > View this message in context:http://r.789695.n4.nabble.com/Using-a-sting-in-variable-names-tp3527318p3527318.html> Sent from the R help mailing list archive at Nabble.com. > >why don't you use a list (?list)? This is particularly useful if each file is formatted in the same way (ie. if they have the same columns, in the same order, etc.), but this is not a necessity. Read each file as an separate item in the list. That way if you want to compute the same statistic on each of your files, you can use lapply on the each item of the list. I think this way save memory, but not totally sure on it. Example: X <- 1:100 vpn <- vector(mode = "list", length = length(X)) for(i in 1:length(X)){ vpn[[i]] <- read.table(paste("vpn", X[i], ".dat", sep ="")) } HTH, Ken