Opening all files in a folder, and applying a function
I am doing a relatively simple piece of analysis which I have put into a function, on all the files in a particular folder. I was wondering whether anyone had any tips to help me automate the process on a number of different folders.
- Firstly, I was wondering whether there was a way of reading all the files in a particular folder straight into R. I believe the following command will list all the files:
files <- (Sys.glob("*.csv"))
...which I found from Using R to list all files with a specified extension
And then the following code reads all those files into R.
listOfFiles <- lapply(files, function(x) read.table(x, header = FALSE))
…from Manipulating multiple files in R
But the files seem to be read in as one continuous list and not individual files… how can I change the script to open all the csv files in a particular folder as individual dataframes?
Secondly, assuming that I can read all the files in separately, how do I complete a function on all these dataframes in one go. For example, I have created four small dataframes so I can illustrate what I want:
Df.1 <- data.frame(A = c(5,4,7,6,8,4),B = (c(1,5,2,4,9,1)))
Df.2 <- data.frame(A = c(1:6),B = (c(2,3,4,5,1,1)))
Df.3 <- data.frame(A = c(4,6,8,0,1,11),B = (c(7,6,5,9,1,15)))
Df.4 <- data.frame(A = c(4,2,6,8,1,0),B = (c(3,1,9,11,2,16)))
I have also made up an example function:
Summary<-function(dfile){
SumA<-sum(dfile$A)
MinA<-min(dfile$A)
MeanA<-mean(dfile$A)
MedianA<-median(dfile$A)
MaxA<-max(dfile$A)
sumB<-sum(dfile$B)
MinB<-min(dfile$B)
MeanB<-mean(dfile$B)
MedianB<-median(dfile$B)
MaxB<-max(dfile$B)
Sum<-c(sumA,sumB)
Min<-c(MinA,MinB)
Mean<-c(MeanA,MeanB)
Median<-c(MedianA,MedianB)
Max<-c(MaxA,MaxB)
rm(sumA,sumB,MinA,MinB,MeanA,MeanB,MedianA,MedianB,MaxA,MaxB)
Label<-c("A","B")
dfile_summary<-data.frame(Label,Sum,Min,Mean,Median,Max)
return(dfile_summary)}
I would ordinarily use the following command to apply the function to each individual dataframe.
Df1.summary<-Summary(dfile)
Is there a way instead of applying the function to all the dataframes, and use the titles of the dataframes in the summary tables (i.e. Df1.summary).
Many thanks,
Katie
r
add a comment |
I am doing a relatively simple piece of analysis which I have put into a function, on all the files in a particular folder. I was wondering whether anyone had any tips to help me automate the process on a number of different folders.
- Firstly, I was wondering whether there was a way of reading all the files in a particular folder straight into R. I believe the following command will list all the files:
files <- (Sys.glob("*.csv"))
...which I found from Using R to list all files with a specified extension
And then the following code reads all those files into R.
listOfFiles <- lapply(files, function(x) read.table(x, header = FALSE))
…from Manipulating multiple files in R
But the files seem to be read in as one continuous list and not individual files… how can I change the script to open all the csv files in a particular folder as individual dataframes?
Secondly, assuming that I can read all the files in separately, how do I complete a function on all these dataframes in one go. For example, I have created four small dataframes so I can illustrate what I want:
Df.1 <- data.frame(A = c(5,4,7,6,8,4),B = (c(1,5,2,4,9,1)))
Df.2 <- data.frame(A = c(1:6),B = (c(2,3,4,5,1,1)))
Df.3 <- data.frame(A = c(4,6,8,0,1,11),B = (c(7,6,5,9,1,15)))
Df.4 <- data.frame(A = c(4,2,6,8,1,0),B = (c(3,1,9,11,2,16)))
I have also made up an example function:
Summary<-function(dfile){
SumA<-sum(dfile$A)
MinA<-min(dfile$A)
MeanA<-mean(dfile$A)
MedianA<-median(dfile$A)
MaxA<-max(dfile$A)
sumB<-sum(dfile$B)
MinB<-min(dfile$B)
MeanB<-mean(dfile$B)
MedianB<-median(dfile$B)
MaxB<-max(dfile$B)
Sum<-c(sumA,sumB)
Min<-c(MinA,MinB)
Mean<-c(MeanA,MeanB)
Median<-c(MedianA,MedianB)
Max<-c(MaxA,MaxB)
rm(sumA,sumB,MinA,MinB,MeanA,MeanB,MedianA,MedianB,MaxA,MaxB)
Label<-c("A","B")
dfile_summary<-data.frame(Label,Sum,Min,Mean,Median,Max)
return(dfile_summary)}
I would ordinarily use the following command to apply the function to each individual dataframe.
Df1.summary<-Summary(dfile)
Is there a way instead of applying the function to all the dataframes, and use the titles of the dataframes in the summary tables (i.e. Df1.summary).
Many thanks,
Katie
r
add a comment |
I am doing a relatively simple piece of analysis which I have put into a function, on all the files in a particular folder. I was wondering whether anyone had any tips to help me automate the process on a number of different folders.
- Firstly, I was wondering whether there was a way of reading all the files in a particular folder straight into R. I believe the following command will list all the files:
files <- (Sys.glob("*.csv"))
...which I found from Using R to list all files with a specified extension
And then the following code reads all those files into R.
listOfFiles <- lapply(files, function(x) read.table(x, header = FALSE))
…from Manipulating multiple files in R
But the files seem to be read in as one continuous list and not individual files… how can I change the script to open all the csv files in a particular folder as individual dataframes?
Secondly, assuming that I can read all the files in separately, how do I complete a function on all these dataframes in one go. For example, I have created four small dataframes so I can illustrate what I want:
Df.1 <- data.frame(A = c(5,4,7,6,8,4),B = (c(1,5,2,4,9,1)))
Df.2 <- data.frame(A = c(1:6),B = (c(2,3,4,5,1,1)))
Df.3 <- data.frame(A = c(4,6,8,0,1,11),B = (c(7,6,5,9,1,15)))
Df.4 <- data.frame(A = c(4,2,6,8,1,0),B = (c(3,1,9,11,2,16)))
I have also made up an example function:
Summary<-function(dfile){
SumA<-sum(dfile$A)
MinA<-min(dfile$A)
MeanA<-mean(dfile$A)
MedianA<-median(dfile$A)
MaxA<-max(dfile$A)
sumB<-sum(dfile$B)
MinB<-min(dfile$B)
MeanB<-mean(dfile$B)
MedianB<-median(dfile$B)
MaxB<-max(dfile$B)
Sum<-c(sumA,sumB)
Min<-c(MinA,MinB)
Mean<-c(MeanA,MeanB)
Median<-c(MedianA,MedianB)
Max<-c(MaxA,MaxB)
rm(sumA,sumB,MinA,MinB,MeanA,MeanB,MedianA,MedianB,MaxA,MaxB)
Label<-c("A","B")
dfile_summary<-data.frame(Label,Sum,Min,Mean,Median,Max)
return(dfile_summary)}
I would ordinarily use the following command to apply the function to each individual dataframe.
Df1.summary<-Summary(dfile)
Is there a way instead of applying the function to all the dataframes, and use the titles of the dataframes in the summary tables (i.e. Df1.summary).
Many thanks,
Katie
r
I am doing a relatively simple piece of analysis which I have put into a function, on all the files in a particular folder. I was wondering whether anyone had any tips to help me automate the process on a number of different folders.
- Firstly, I was wondering whether there was a way of reading all the files in a particular folder straight into R. I believe the following command will list all the files:
files <- (Sys.glob("*.csv"))
...which I found from Using R to list all files with a specified extension
And then the following code reads all those files into R.
listOfFiles <- lapply(files, function(x) read.table(x, header = FALSE))
…from Manipulating multiple files in R
But the files seem to be read in as one continuous list and not individual files… how can I change the script to open all the csv files in a particular folder as individual dataframes?
Secondly, assuming that I can read all the files in separately, how do I complete a function on all these dataframes in one go. For example, I have created four small dataframes so I can illustrate what I want:
Df.1 <- data.frame(A = c(5,4,7,6,8,4),B = (c(1,5,2,4,9,1)))
Df.2 <- data.frame(A = c(1:6),B = (c(2,3,4,5,1,1)))
Df.3 <- data.frame(A = c(4,6,8,0,1,11),B = (c(7,6,5,9,1,15)))
Df.4 <- data.frame(A = c(4,2,6,8,1,0),B = (c(3,1,9,11,2,16)))
I have also made up an example function:
Summary<-function(dfile){
SumA<-sum(dfile$A)
MinA<-min(dfile$A)
MeanA<-mean(dfile$A)
MedianA<-median(dfile$A)
MaxA<-max(dfile$A)
sumB<-sum(dfile$B)
MinB<-min(dfile$B)
MeanB<-mean(dfile$B)
MedianB<-median(dfile$B)
MaxB<-max(dfile$B)
Sum<-c(sumA,sumB)
Min<-c(MinA,MinB)
Mean<-c(MeanA,MeanB)
Median<-c(MedianA,MedianB)
Max<-c(MaxA,MaxB)
rm(sumA,sumB,MinA,MinB,MeanA,MeanB,MedianA,MedianB,MaxA,MaxB)
Label<-c("A","B")
dfile_summary<-data.frame(Label,Sum,Min,Mean,Median,Max)
return(dfile_summary)}
I would ordinarily use the following command to apply the function to each individual dataframe.
Df1.summary<-Summary(dfile)
Is there a way instead of applying the function to all the dataframes, and use the titles of the dataframes in the summary tables (i.e. Df1.summary).
Many thanks,
Katie
r
r
edited May 23 '17 at 12:26
Community♦
11
11
asked Mar 5 '12 at 9:44
KT_1KT_1
2,587103452
2,587103452
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
On the contrary, I do think working with list makes it easy to automate such things.
Here is one solution (I stored your four dataframes in folder temp/).
filenames <- list.files("temp", pattern="*.csv", full.names=TRUE)
ldf <- lapply(filenames, read.csv)
res <- lapply(ldf, summary)
names(res) <- substr(filenames, 6, 30)
It is important to store the full path for your files (as I did with full.names), otherwise you have to paste the working directory, e.g.
filenames <- list.files("temp", pattern="*.csv")
paste("temp", filenames, sep="/")
will work too. Note that I used substr to extract file names while discarding full path.
You can access your summary tables as follows:
> res$`df4.csv`
A B
Min. :0.00 Min. : 1.00
1st Qu.:1.25 1st Qu.: 2.25
Median :3.00 Median : 6.00
Mean :3.50 Mean : 7.00
3rd Qu.:5.50 3rd Qu.:10.50
Max. :8.00 Max. :16.00
If you really want to get individual summary tables, you can extract them afterwards. E.g.,
for (i in 1:length(res))
assign(paste(paste("df", i, sep=""), "summary", sep="."), res[[i]])
3
+1 I wouldplyr::llply(orldply) instead oflapplyto preserve the names throughout, and define my own summary function, e.g.plyr::each(min, max, mean, sd, median)
– baptiste
Mar 5 '12 at 10:45
+1 @chl: thanks for the fullnames trick in list.files function....i forgot it in my answer !!!
– dickoa
Mar 5 '12 at 10:51
@baptiste (+1) Thanks for theplyrsuggestion.
– chl
Mar 5 '12 at 11:07
Thanks @chl. How do I use the above code with a function that I have written? The example function that I used above ("Summary")with sum, mean, median etc. was just used as an example that I created quickly - the real function that I am using for my actual analysis is much more complex. Any ideas of how I incorporate a more complex function into the above code to give the same individual summary tables? –
– KT_1
Mar 5 '12 at 14:59
@Katie I guess you can replacesummarywith any function of yours, provided it takes a data.frame as an argument (and/or optional parameters that are constant across the difference DFs). E.g.,lapply(ldf, function(x) apply(x, 2, function(x) c(mean(x), sd(x))))would return mean and SD computed colwise.
– chl
Mar 5 '12 at 15:29
|
show 1 more comment
usually i don't use for loop in R, but here is my solution using for loops and two packages : plyr and dostats
plyr is on cran and you can download dostats on https://github.com/halpo/dostats (may be using install_github from Hadley devtools package)
Assuming that i have your first two data.frame (Df.1 and Df.2) in csv files, you can do something like this.
require(plyr)
require(dostats)
files <- list.files(pattern = ".csv")
for (i in seq_along(files)) {
assign(paste("Df", i, sep = "."), read.csv(files[i]))
assign(paste(paste("Df", i, sep = ""), "summary", sep = "."),
ldply(get(paste("Df", i, sep = ".")), dostats, sum, min, mean, median, max))
}
Here is the output
R> Df1.summary
.id sum min mean median max
1 A 34 4 5.6667 5.5 8
2 B 22 1 3.6667 3.0 9
R> Df2.summary
.id sum min mean median max
1 A 21 1 3.5000 3.5 6
2 B 16 1 2.6667 2.5 5
(+1) It looks like we answered quite at the same time and yourplyrsolution is quite nice!
– chl
Mar 5 '12 at 11:09
1
Thanks @dickoa for your answers. The function that I made up ("Summary") was poorly described. I was just using it for illustrative purposes - my real function is much more complicated so I was wondering how the above code (and probably my function) could be changed so that it is applied for all the different data frames (and doesn't just use the in built functions in R).
– KT_1
Mar 5 '12 at 11:58
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f9564489%2fopening-all-files-in-a-folder-and-applying-a-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
On the contrary, I do think working with list makes it easy to automate such things.
Here is one solution (I stored your four dataframes in folder temp/).
filenames <- list.files("temp", pattern="*.csv", full.names=TRUE)
ldf <- lapply(filenames, read.csv)
res <- lapply(ldf, summary)
names(res) <- substr(filenames, 6, 30)
It is important to store the full path for your files (as I did with full.names), otherwise you have to paste the working directory, e.g.
filenames <- list.files("temp", pattern="*.csv")
paste("temp", filenames, sep="/")
will work too. Note that I used substr to extract file names while discarding full path.
You can access your summary tables as follows:
> res$`df4.csv`
A B
Min. :0.00 Min. : 1.00
1st Qu.:1.25 1st Qu.: 2.25
Median :3.00 Median : 6.00
Mean :3.50 Mean : 7.00
3rd Qu.:5.50 3rd Qu.:10.50
Max. :8.00 Max. :16.00
If you really want to get individual summary tables, you can extract them afterwards. E.g.,
for (i in 1:length(res))
assign(paste(paste("df", i, sep=""), "summary", sep="."), res[[i]])
3
+1 I wouldplyr::llply(orldply) instead oflapplyto preserve the names throughout, and define my own summary function, e.g.plyr::each(min, max, mean, sd, median)
– baptiste
Mar 5 '12 at 10:45
+1 @chl: thanks for the fullnames trick in list.files function....i forgot it in my answer !!!
– dickoa
Mar 5 '12 at 10:51
@baptiste (+1) Thanks for theplyrsuggestion.
– chl
Mar 5 '12 at 11:07
Thanks @chl. How do I use the above code with a function that I have written? The example function that I used above ("Summary")with sum, mean, median etc. was just used as an example that I created quickly - the real function that I am using for my actual analysis is much more complex. Any ideas of how I incorporate a more complex function into the above code to give the same individual summary tables? –
– KT_1
Mar 5 '12 at 14:59
@Katie I guess you can replacesummarywith any function of yours, provided it takes a data.frame as an argument (and/or optional parameters that are constant across the difference DFs). E.g.,lapply(ldf, function(x) apply(x, 2, function(x) c(mean(x), sd(x))))would return mean and SD computed colwise.
– chl
Mar 5 '12 at 15:29
|
show 1 more comment
On the contrary, I do think working with list makes it easy to automate such things.
Here is one solution (I stored your four dataframes in folder temp/).
filenames <- list.files("temp", pattern="*.csv", full.names=TRUE)
ldf <- lapply(filenames, read.csv)
res <- lapply(ldf, summary)
names(res) <- substr(filenames, 6, 30)
It is important to store the full path for your files (as I did with full.names), otherwise you have to paste the working directory, e.g.
filenames <- list.files("temp", pattern="*.csv")
paste("temp", filenames, sep="/")
will work too. Note that I used substr to extract file names while discarding full path.
You can access your summary tables as follows:
> res$`df4.csv`
A B
Min. :0.00 Min. : 1.00
1st Qu.:1.25 1st Qu.: 2.25
Median :3.00 Median : 6.00
Mean :3.50 Mean : 7.00
3rd Qu.:5.50 3rd Qu.:10.50
Max. :8.00 Max. :16.00
If you really want to get individual summary tables, you can extract them afterwards. E.g.,
for (i in 1:length(res))
assign(paste(paste("df", i, sep=""), "summary", sep="."), res[[i]])
3
+1 I wouldplyr::llply(orldply) instead oflapplyto preserve the names throughout, and define my own summary function, e.g.plyr::each(min, max, mean, sd, median)
– baptiste
Mar 5 '12 at 10:45
+1 @chl: thanks for the fullnames trick in list.files function....i forgot it in my answer !!!
– dickoa
Mar 5 '12 at 10:51
@baptiste (+1) Thanks for theplyrsuggestion.
– chl
Mar 5 '12 at 11:07
Thanks @chl. How do I use the above code with a function that I have written? The example function that I used above ("Summary")with sum, mean, median etc. was just used as an example that I created quickly - the real function that I am using for my actual analysis is much more complex. Any ideas of how I incorporate a more complex function into the above code to give the same individual summary tables? –
– KT_1
Mar 5 '12 at 14:59
@Katie I guess you can replacesummarywith any function of yours, provided it takes a data.frame as an argument (and/or optional parameters that are constant across the difference DFs). E.g.,lapply(ldf, function(x) apply(x, 2, function(x) c(mean(x), sd(x))))would return mean and SD computed colwise.
– chl
Mar 5 '12 at 15:29
|
show 1 more comment
On the contrary, I do think working with list makes it easy to automate such things.
Here is one solution (I stored your four dataframes in folder temp/).
filenames <- list.files("temp", pattern="*.csv", full.names=TRUE)
ldf <- lapply(filenames, read.csv)
res <- lapply(ldf, summary)
names(res) <- substr(filenames, 6, 30)
It is important to store the full path for your files (as I did with full.names), otherwise you have to paste the working directory, e.g.
filenames <- list.files("temp", pattern="*.csv")
paste("temp", filenames, sep="/")
will work too. Note that I used substr to extract file names while discarding full path.
You can access your summary tables as follows:
> res$`df4.csv`
A B
Min. :0.00 Min. : 1.00
1st Qu.:1.25 1st Qu.: 2.25
Median :3.00 Median : 6.00
Mean :3.50 Mean : 7.00
3rd Qu.:5.50 3rd Qu.:10.50
Max. :8.00 Max. :16.00
If you really want to get individual summary tables, you can extract them afterwards. E.g.,
for (i in 1:length(res))
assign(paste(paste("df", i, sep=""), "summary", sep="."), res[[i]])
On the contrary, I do think working with list makes it easy to automate such things.
Here is one solution (I stored your four dataframes in folder temp/).
filenames <- list.files("temp", pattern="*.csv", full.names=TRUE)
ldf <- lapply(filenames, read.csv)
res <- lapply(ldf, summary)
names(res) <- substr(filenames, 6, 30)
It is important to store the full path for your files (as I did with full.names), otherwise you have to paste the working directory, e.g.
filenames <- list.files("temp", pattern="*.csv")
paste("temp", filenames, sep="/")
will work too. Note that I used substr to extract file names while discarding full path.
You can access your summary tables as follows:
> res$`df4.csv`
A B
Min. :0.00 Min. : 1.00
1st Qu.:1.25 1st Qu.: 2.25
Median :3.00 Median : 6.00
Mean :3.50 Mean : 7.00
3rd Qu.:5.50 3rd Qu.:10.50
Max. :8.00 Max. :16.00
If you really want to get individual summary tables, you can extract them afterwards. E.g.,
for (i in 1:length(res))
assign(paste(paste("df", i, sep=""), "summary", sep="."), res[[i]])
edited Mar 5 '12 at 11:13
answered Mar 5 '12 at 10:29
chlchl
20k44169
20k44169
3
+1 I wouldplyr::llply(orldply) instead oflapplyto preserve the names throughout, and define my own summary function, e.g.plyr::each(min, max, mean, sd, median)
– baptiste
Mar 5 '12 at 10:45
+1 @chl: thanks for the fullnames trick in list.files function....i forgot it in my answer !!!
– dickoa
Mar 5 '12 at 10:51
@baptiste (+1) Thanks for theplyrsuggestion.
– chl
Mar 5 '12 at 11:07
Thanks @chl. How do I use the above code with a function that I have written? The example function that I used above ("Summary")with sum, mean, median etc. was just used as an example that I created quickly - the real function that I am using for my actual analysis is much more complex. Any ideas of how I incorporate a more complex function into the above code to give the same individual summary tables? –
– KT_1
Mar 5 '12 at 14:59
@Katie I guess you can replacesummarywith any function of yours, provided it takes a data.frame as an argument (and/or optional parameters that are constant across the difference DFs). E.g.,lapply(ldf, function(x) apply(x, 2, function(x) c(mean(x), sd(x))))would return mean and SD computed colwise.
– chl
Mar 5 '12 at 15:29
|
show 1 more comment
3
+1 I wouldplyr::llply(orldply) instead oflapplyto preserve the names throughout, and define my own summary function, e.g.plyr::each(min, max, mean, sd, median)
– baptiste
Mar 5 '12 at 10:45
+1 @chl: thanks for the fullnames trick in list.files function....i forgot it in my answer !!!
– dickoa
Mar 5 '12 at 10:51
@baptiste (+1) Thanks for theplyrsuggestion.
– chl
Mar 5 '12 at 11:07
Thanks @chl. How do I use the above code with a function that I have written? The example function that I used above ("Summary")with sum, mean, median etc. was just used as an example that I created quickly - the real function that I am using for my actual analysis is much more complex. Any ideas of how I incorporate a more complex function into the above code to give the same individual summary tables? –
– KT_1
Mar 5 '12 at 14:59
@Katie I guess you can replacesummarywith any function of yours, provided it takes a data.frame as an argument (and/or optional parameters that are constant across the difference DFs). E.g.,lapply(ldf, function(x) apply(x, 2, function(x) c(mean(x), sd(x))))would return mean and SD computed colwise.
– chl
Mar 5 '12 at 15:29
3
3
+1 I would
plyr::llply (or ldply) instead of lapply to preserve the names throughout, and define my own summary function, e.g. plyr::each(min, max, mean, sd, median)– baptiste
Mar 5 '12 at 10:45
+1 I would
plyr::llply (or ldply) instead of lapply to preserve the names throughout, and define my own summary function, e.g. plyr::each(min, max, mean, sd, median)– baptiste
Mar 5 '12 at 10:45
+1 @chl: thanks for the fullnames trick in list.files function....i forgot it in my answer !!!
– dickoa
Mar 5 '12 at 10:51
+1 @chl: thanks for the fullnames trick in list.files function....i forgot it in my answer !!!
– dickoa
Mar 5 '12 at 10:51
@baptiste (+1) Thanks for the
plyr suggestion.– chl
Mar 5 '12 at 11:07
@baptiste (+1) Thanks for the
plyr suggestion.– chl
Mar 5 '12 at 11:07
Thanks @chl. How do I use the above code with a function that I have written? The example function that I used above ("Summary")with sum, mean, median etc. was just used as an example that I created quickly - the real function that I am using for my actual analysis is much more complex. Any ideas of how I incorporate a more complex function into the above code to give the same individual summary tables? –
– KT_1
Mar 5 '12 at 14:59
Thanks @chl. How do I use the above code with a function that I have written? The example function that I used above ("Summary")with sum, mean, median etc. was just used as an example that I created quickly - the real function that I am using for my actual analysis is much more complex. Any ideas of how I incorporate a more complex function into the above code to give the same individual summary tables? –
– KT_1
Mar 5 '12 at 14:59
@Katie I guess you can replace
summary with any function of yours, provided it takes a data.frame as an argument (and/or optional parameters that are constant across the difference DFs). E.g., lapply(ldf, function(x) apply(x, 2, function(x) c(mean(x), sd(x)))) would return mean and SD computed colwise.– chl
Mar 5 '12 at 15:29
@Katie I guess you can replace
summary with any function of yours, provided it takes a data.frame as an argument (and/or optional parameters that are constant across the difference DFs). E.g., lapply(ldf, function(x) apply(x, 2, function(x) c(mean(x), sd(x)))) would return mean and SD computed colwise.– chl
Mar 5 '12 at 15:29
|
show 1 more comment
usually i don't use for loop in R, but here is my solution using for loops and two packages : plyr and dostats
plyr is on cran and you can download dostats on https://github.com/halpo/dostats (may be using install_github from Hadley devtools package)
Assuming that i have your first two data.frame (Df.1 and Df.2) in csv files, you can do something like this.
require(plyr)
require(dostats)
files <- list.files(pattern = ".csv")
for (i in seq_along(files)) {
assign(paste("Df", i, sep = "."), read.csv(files[i]))
assign(paste(paste("Df", i, sep = ""), "summary", sep = "."),
ldply(get(paste("Df", i, sep = ".")), dostats, sum, min, mean, median, max))
}
Here is the output
R> Df1.summary
.id sum min mean median max
1 A 34 4 5.6667 5.5 8
2 B 22 1 3.6667 3.0 9
R> Df2.summary
.id sum min mean median max
1 A 21 1 3.5000 3.5 6
2 B 16 1 2.6667 2.5 5
(+1) It looks like we answered quite at the same time and yourplyrsolution is quite nice!
– chl
Mar 5 '12 at 11:09
1
Thanks @dickoa for your answers. The function that I made up ("Summary") was poorly described. I was just using it for illustrative purposes - my real function is much more complicated so I was wondering how the above code (and probably my function) could be changed so that it is applied for all the different data frames (and doesn't just use the in built functions in R).
– KT_1
Mar 5 '12 at 11:58
add a comment |
usually i don't use for loop in R, but here is my solution using for loops and two packages : plyr and dostats
plyr is on cran and you can download dostats on https://github.com/halpo/dostats (may be using install_github from Hadley devtools package)
Assuming that i have your first two data.frame (Df.1 and Df.2) in csv files, you can do something like this.
require(plyr)
require(dostats)
files <- list.files(pattern = ".csv")
for (i in seq_along(files)) {
assign(paste("Df", i, sep = "."), read.csv(files[i]))
assign(paste(paste("Df", i, sep = ""), "summary", sep = "."),
ldply(get(paste("Df", i, sep = ".")), dostats, sum, min, mean, median, max))
}
Here is the output
R> Df1.summary
.id sum min mean median max
1 A 34 4 5.6667 5.5 8
2 B 22 1 3.6667 3.0 9
R> Df2.summary
.id sum min mean median max
1 A 21 1 3.5000 3.5 6
2 B 16 1 2.6667 2.5 5
(+1) It looks like we answered quite at the same time and yourplyrsolution is quite nice!
– chl
Mar 5 '12 at 11:09
1
Thanks @dickoa for your answers. The function that I made up ("Summary") was poorly described. I was just using it for illustrative purposes - my real function is much more complicated so I was wondering how the above code (and probably my function) could be changed so that it is applied for all the different data frames (and doesn't just use the in built functions in R).
– KT_1
Mar 5 '12 at 11:58
add a comment |
usually i don't use for loop in R, but here is my solution using for loops and two packages : plyr and dostats
plyr is on cran and you can download dostats on https://github.com/halpo/dostats (may be using install_github from Hadley devtools package)
Assuming that i have your first two data.frame (Df.1 and Df.2) in csv files, you can do something like this.
require(plyr)
require(dostats)
files <- list.files(pattern = ".csv")
for (i in seq_along(files)) {
assign(paste("Df", i, sep = "."), read.csv(files[i]))
assign(paste(paste("Df", i, sep = ""), "summary", sep = "."),
ldply(get(paste("Df", i, sep = ".")), dostats, sum, min, mean, median, max))
}
Here is the output
R> Df1.summary
.id sum min mean median max
1 A 34 4 5.6667 5.5 8
2 B 22 1 3.6667 3.0 9
R> Df2.summary
.id sum min mean median max
1 A 21 1 3.5000 3.5 6
2 B 16 1 2.6667 2.5 5
usually i don't use for loop in R, but here is my solution using for loops and two packages : plyr and dostats
plyr is on cran and you can download dostats on https://github.com/halpo/dostats (may be using install_github from Hadley devtools package)
Assuming that i have your first two data.frame (Df.1 and Df.2) in csv files, you can do something like this.
require(plyr)
require(dostats)
files <- list.files(pattern = ".csv")
for (i in seq_along(files)) {
assign(paste("Df", i, sep = "."), read.csv(files[i]))
assign(paste(paste("Df", i, sep = ""), "summary", sep = "."),
ldply(get(paste("Df", i, sep = ".")), dostats, sum, min, mean, median, max))
}
Here is the output
R> Df1.summary
.id sum min mean median max
1 A 34 4 5.6667 5.5 8
2 B 22 1 3.6667 3.0 9
R> Df2.summary
.id sum min mean median max
1 A 21 1 3.5000 3.5 6
2 B 16 1 2.6667 2.5 5
answered Mar 5 '12 at 10:32
dickoadickoa
15.2k33144
15.2k33144
(+1) It looks like we answered quite at the same time and yourplyrsolution is quite nice!
– chl
Mar 5 '12 at 11:09
1
Thanks @dickoa for your answers. The function that I made up ("Summary") was poorly described. I was just using it for illustrative purposes - my real function is much more complicated so I was wondering how the above code (and probably my function) could be changed so that it is applied for all the different data frames (and doesn't just use the in built functions in R).
– KT_1
Mar 5 '12 at 11:58
add a comment |
(+1) It looks like we answered quite at the same time and yourplyrsolution is quite nice!
– chl
Mar 5 '12 at 11:09
1
Thanks @dickoa for your answers. The function that I made up ("Summary") was poorly described. I was just using it for illustrative purposes - my real function is much more complicated so I was wondering how the above code (and probably my function) could be changed so that it is applied for all the different data frames (and doesn't just use the in built functions in R).
– KT_1
Mar 5 '12 at 11:58
(+1) It looks like we answered quite at the same time and your
plyr solution is quite nice!– chl
Mar 5 '12 at 11:09
(+1) It looks like we answered quite at the same time and your
plyr solution is quite nice!– chl
Mar 5 '12 at 11:09
1
1
Thanks @dickoa for your answers. The function that I made up ("Summary") was poorly described. I was just using it for illustrative purposes - my real function is much more complicated so I was wondering how the above code (and probably my function) could be changed so that it is applied for all the different data frames (and doesn't just use the in built functions in R).
– KT_1
Mar 5 '12 at 11:58
Thanks @dickoa for your answers. The function that I made up ("Summary") was poorly described. I was just using it for illustrative purposes - my real function is much more complicated so I was wondering how the above code (and probably my function) could be changed so that it is applied for all the different data frames (and doesn't just use the in built functions in R).
– KT_1
Mar 5 '12 at 11:58
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f9564489%2fopening-all-files-in-a-folder-and-applying-a-function%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown