문제

I am relatively new to R so bear with me. I have 50+ csv files and am looking to run through each of them and grab each column's unique values. They are all formatted with first row being the headers.

The ideal output would then be a data frame indicating filename, column headers, and unique values for each csv. These are unique values for each column, one at a time, not for any uniqueness across a combination of columns.

Any help would be greatly appreciated!

Here is how I'm getting unique values as a list, but I'm not sure what to do next:

lapply(files, function(x) {
  t <- read.csv(x, header=TRUE) # load file
  unq <- apply(t, 2, unique)
})
도움이 되었습니까?

해결책

This should do the trick:

do.call(rbind, lapply(files, function(x) {
  dat <- read.csv(x, header=TRUE)
  do.call(rbind, lapply(seq(ncol(dat)), function(idx) {
    data.frame(filename=x, column=colnames(dat)[idx],
               value=unique(dat[,idx]))
  }))
}))

The outer lapply returns a data frame for each of your files x, and the inner lapply returns a data frame for each column numbered idx within x.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top