3

This is a part of a larger script- long story short, input to R is a semi-colon separated path to the location of my text files. All the text files are of the following format:

File1:

Name1   3
Name2   4
Name3   55

File2:

Name1   4
Name2   33
Name3   102

File3:

Name1   12
Name2   2
Name5   33

Here is an example:

cond1<-'/User/Desktop/File1;/User/Desktop/File2;/User/Desktop/File3'

#separates the elements
normalList<-strsplit(cond1, ";")

#now you can access each element 
nor<-unlist(normalList)

baseNorm<-vector("list", length(nor))
dirNorm<-vector("list", length(nor))
pathNorm<-vector("list", length(nor))
Norm<-vector("list", length(nor))
new<-vector("list", length(nor))
for (i in 1:length(nor))
{
  baseNorm[[i]] <- basename(nor[i])
  dirNorm[[i]]<-dirname(nor[i])
  pathNorm[[i]]<-paste(dirNorm[[i]], baseNorm[[i]], sep="/")
  Norm[[i]]<-read.delim(pathNorm[[i]], header=F)

}

In the example here the input to R contains 3 files- however I need the script to be flexible enough that it can work for any number of files passed, hence the attempt with the for loop. The BIG IDEA is: 1) Get the path to the specific file for all files. 2) Load the text files into R 3) Create a new file that contains the common first column (Name1, Name2, Name3). All the next columns in this new file correspond to the 2nd column of File1, File2, File3, File4, etc. Essentially, I need to create a master file:

Name1   3   4   55
Name2   4   33  102
Name3   12  2   33

I am sure there is a much simpler solution even to the code I already have, since with all the [[]] I don't even know how to start writing a function that creates the master file. I just started using R, so any input is a valuable learning experience, and thank you in advance!

1
  • list.files (see argument pattern), lapply and do.call are the keywords. Commented Mar 21, 2013 at 23:18

2 Answers 2

3

Something like this?? :

fileNames <- list.files()

master1 <- read.csv(file = fileNames[1], sep = ";", header = T)    

for(i in 2:length(fileNames)){    
file1 <- read.csv(file = fileNames[i], sep = ";", header = T)
master1$newCol1 <- file1[,2]
colnames(master1)[ncol(master1)] <- paste("file",i,sep = "")
}
Sign up to request clarification or add additional context in comments.

5 Comments

Why not use [[ instead of $, then you don't have to muck around with colnames<-
I'm not familiar with '[['. What does it do?
See ?"[[" -- and library(fortunes);fortune(312)
Thanks, I was not aware of that. I'd only used double brackets when handling explicit named lists (as opposed to data.frames which are a type of list if I understand correctly). How would you use '[[' in the above example?
a data.frame is a named list master1[[paste("file",i,sep = "")]] <- file1[,2]
1

I realize I may have been a bit vague. Here's some semi-pseudo-code that might help.

my.files <- list.files(pattern = ".csv")
imported.files <- sapply(my.files, read.csv, ...) # additional paramaters for proper import
out <- do.call("cbind", imported.files)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.