0

I am a new user to R.I have already imported all data from all my txt file using the code down below,but i want to create a new variable when importing data,the variable is called case.The value of case for the first row is 1 and for the rest is 0.

And when i try to run the code,the console did not say anytime wrong ,the data has been imported, but the new variable wasn't created.I don't know why.

for(i in Filenames){
  perpos <- which(strsplit(i, "")[[1]]==".")
  data=assign(
    gsub(" ","",substr(i, 1, perpos-1)),
    read.table(paste(filepath,i,sep=""),fill=TRUE,header=TRUE,quote ="",row.names = NULL,sep="\t")
  )
  strsplit(i, "")
  filename  = strsplit(as.character(i),"\\.txt")
  data$case = ifelse(data$NAME=="filename",1,0)
} 
3
  • Don't use data = assign(...); the purpose of assign is not its return value. Your strsplit(i,"") in contrast does need to be assigned somewhere. Commented Jul 22, 2015 at 16:34
  • Are you trying to match data$NAME with the variable filename above it or the literal string "filename". Commented Jul 22, 2015 at 16:36
  • not the literal string "filename",data$NAME is a list of names,the first one is case,others are controls.And i forget to deletestrsplit(i,"") Commented Jul 22, 2015 at 16:45

2 Answers 2

1

Thanks guys! I used @joosts's code and made some ajustment. The code down below works just fine.

fn <- paste(filepath,Filenames,sep="")
mylist <- lapply(fn, read.table,fill = TRUE, header = TRUE, quote = "",row.names = NULL, sep = "\t",stringsAsFactors=FALSE)
for(i in 1:length(Filenames)){
mylist[[i]]<- cbind(mylist[[i]], case = 0)
if(nrow(mylist[[i]])>0) {
mylist[[i]]$case[1] <- 1
}
mylist[[i]]<- cbind(mylist[[i]], ID = i)
}
do.call(rbind, mylist) 
Sign up to request clarification or add additional context in comments.

Comments

0

I am assuming you want to read in multiple text files, with each file containing the same columns (in the same order). In order to combine multiple dataframes (the things that result from calling read.data()), you should call the function rbind(). And I assume your code to get a filename without the extension is slightly overcomplex...

for(file in filenames) {
 sanitized_filename <- gsub(" ", "", strsplit(file, "\\.")[[1]][1])
 file.frame <- read.table(paste(filepath, file, sep=""), fill = TRUE, header = TRUE, quote = "", row.names = NULL, sep = "\t")
 file.frame <- cbind(file.frame, name = I(sanitized_filename), case = 0)
 if(nrow(file.frame)>0) {
   file.frame$case[1] <- 1
 }
 data <- ifelse(exists("data"), rbind(data, file.frame), file.frame)
}

10 Comments

joosts,thanks a lot! But when i try to run the code ,it saysError in rep(xi, length.out = nvar) : attempt to replicate an object of type 'closure',why?
Not really sure... if you could show me some of data files I could improve the code above... (missing any input I just had to make this one up without testing).
i'm sorry i can't show you my file,it's data collected from patients. what do you mean by` ifelse(exists("data"), rbind(data, file.frame), file.frame) }`,data wasn't defined before
Exactly if data didnt exist it is created from the new data frame else it is merged with the existing fast using rbind... I probably will be able to help you with two fake data files as well.
how can i send you the file? btw,i can't read in all the files using your code,only the first one,and the amount of variables became strange....
|

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.