1

I have multiple csv files for which I want to access second column for every file and do a regex which will remove all strings after ";". this Pattern is same for all the files. I have referred this In R, how to get an object's name after it is sent to a function? This is a sample of my file

ID  POLL
1   1,2:ksd ksj 
2   3:jj
3   6:ok0j

This is what I have tried

 setwd("D:/Data/STN")
    temp = list.files(pattern="*.csv")

    for(i in 1:length(temp)){
      DF1=read.csv(temp[i])
      col2=colnames(DF1)[2]
      assign(paste(DF1,"$"),col2)
      DF1$col2 = gsub(":.*","",DF1$col2)

In temp I have all names of all files, I tried with assign but no output. Thanks in advance

Community
  • 1
  • 1
Deepesh
  • 820
  • 1
  • 14
  • 32

1 Answers1

1

We can use lapply to loop over the list of data.frames and replace the suffix part in the second column using sub.

lst1 <- lapply(lst, function(x) {x[,2] <- sub(":.*", "", x[,2])
                        x})

As noted below, the data.frames are read into a list

data

temp <- list.files(pattern="*.csv") 
lst <- lapply(temp, read.csv, stringsAsFactors=FALSE)
Community
  • 1
  • 1
akrun
  • 874,273
  • 37
  • 540
  • 662
  • gives me an error "Error: unexpected ';' in "lapply(lst, function(x) {x[,2] <- sub(":.*", "", x[,2];"" – Deepesh Feb 17 '17 at 12:57
  • @Deepesh A typo, forgot the `)`. Corrected – akrun Feb 17 '17 at 12:59
  • but what is the problem with data frame ,why you used list instead of dataframe. can this be done using dataframe? – Deepesh Feb 17 '17 at 13:04
  • 1
    @Deepesh Yes, of course, you can create data.frame objects in the global environment, but it is not necessary and creates lots of objects and at the end, you may again need some loops to write it back. Instead everything can be done in a `list` – akrun Feb 17 '17 at 13:07