Apply multiple functions in sapply

I have a list of .stat files in the tmp directory.

Example:

a.stat =>

abc,10

abc,20

abc,30

      

b.stat =>

xyz,10

xyz,30

xyz,70

      

etc.

I need to find a summary of all .stat files. I am currently using filelist<-list.files(path="/tmp/",pattern=".stat")

data<-sapply(paste("/tmp/",filelist,sep=''), read.csv, header=FALSE)

However, I need to apply the summary to all files being viewed. Or just in n number of .stat files I need a summary from column 2 column

using

data<-sapply(paste("/tmp/",filelist,sep=''), summary, read.csv, header=FALSE)

doesn't work and gives me a summary with a class symbol, which I don't intend.

sapply(filelist, function(filename){df <- read.csv(filename, header=F);print(summary(df[,2]))})

works fine. However, my overall goal is to find values ​​greater than 2 standard deviations on each side (outliers). So I use sd, but at the same time you need to check if all values ​​in the current file are in the 2SD range.

+3


source to share


1 answer


To use multiple functions at the same time:

f <- function(x){
  list(sum(x),mean(x))
}
sapply(x, f)

      

In your case, you want to apply them consistently, so read the csv data first, then do a short description:



sapply(lapply(paste("/tmp/",filelist,sep=''), read.csv), summary)

      

To enable a subset of your datasets to trigger a summary on a specific column, you can use the external external application feature from summary

to function(x) summary(x[[2]])

.

+4


source







All Articles