I'm quite new to R, sorry if the programming looks bad.
Goal is to create filenames based on a common prefix, i.e. given prefix loop x times to produce prefix-1, prefix-2, prefix-3. And then use these filenames to read.csv(prefix-1,prefix-2, prefix-3).
I've gotten the code to work, but very inefficiently below:
name <- vector(mode="character", length=0)
for (i in 1:numruns)name[i] <- paste(prefix, "-", i, ".log", sep="")
if (numruns == 1) {
raw_data_1 <-read.csv(name[1], header=F, sep="\t", skip=11)
}
if (numruns == 2) {
raw_data_1 <-read.csv(name[1], header=F, sep="\t", skip=11)
raw_data_2 <-read.csv(name[2], header=F, sep="\t", skip=11)
}
if (numruns == 3) {
raw_data_1 <-read.csv(name[1], header=F, sep="\t", skip=11)
raw_data_2 <-read.csv(name[2], header=F, sep="\t", skip=11)
raw_data_3 <-read.csv(name[3], header=F, sep="\t", skip=11) #import files
}
I'm trying to learn how to be more efficient, above works for my purposes but I feel like I should be able wrap it up in the initial loop that produces the names. When I try to modify the original loop I can't get it to work...
for (i in 1:numruns){
name[i] <- paste(prefix, "-", i, ".log", sep="")
raw_data <- paste("raw_data_", i, sep="")
print(raw_data)
raw_data <- read.csv(name[i], header=F, sep="\t", skip=11)
}
Rather than get raw_data_1,raw_data_2,raw_data_3... I get "raw_data". I'm confused because print(raw_data) actually prints "raw_data_1-3" correctly (but only "raw_data" actually contains any information).
Thanks for any help or critique on my code to make it more efficient.