try to create new variable using loop in R,but failed -


i new user r.i have imported data txt file using code down below,but want create new variable when importing data,the variable called case.the value of case first row 1 , rest 0.

and when try run code,the console did not anytime wrong ,the data has been imported, 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) }  

thanks guys! used @joosts's code , made ajustment. code down below works 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)  

Comments

Popular posts from this blog

Fail to load namespace Spring Security http://www.springframework.org/security/tags -

sql - MySQL query optimization using coalesce -

unity3d - Unity local avoidance in user created world -