I am using Rversion 3.03 on a Windows 7 OS and am trying to solve a problem. I am not sure if this is just me being stupid or if this is really a problem with my version of R.

Intitial problem: I have a folder with 300+ csv files and I need to specify a function that reads in a user-specified number of files. So my idea was to use the list.files function to give me a list of the csv's and then choose from this list rather than having to reformat the user input to match the csv filenames.

pm <- function(directory, id = 1:332) {

setwd("C:/Users/cw/Documents")
setwd(directory)

x <- id[1] 
x

files <- list.files() 

#for (x in 1:length(id))
#data[i] <- read.csv(files[x], header=T) 
#}
}

pm("specdata", 25:30)

So first I set the wd which works like a charm. Then I wanted to set x equal to the first element of id to obtain a starting point. Next I wanted to build a vector 'files' to choose the filenames from.

Real problem: if I run the 'pm'-function, R tells me that the object files does not exist. So am I doing sth wrong (obviously I am) and what?

Thanks very much, C

有帮助吗?

解决方案

files is just a local variable that you declare inside your pm function. To use the results in your calling code, you should assign it to a variable (I used filelist here):

filelist <- pm("specdata", 25:30)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top