Hi R users,
I have multiple dataframes in a directory, and with the same postfix
".txt". Each dataframe is a gridded file with just values, but each value
represents one grid cell. There are many years, and each year has 12
months, so there are many such files. For each year, I want to read the
corresponding year's files and add the values of the dataframes, but I
don't know how to do that. For example, the file names are
1990_file01.txt
1990_file02.txt
...
1990_file12.txt
1991_file01.txt
...
1991_file12.txt
...
And my code is like this:
f1990 = list.files("~/directory", pattern = "^1990(.*).txt$")
for(i in 1:length(f1990)){
r1990 = read.table(paste('~/directory',f1990[i],sep='/'),head=F)
}
Could you provide some help on this? Thanks very much.
How to aggregate values in corresponding dataframes
4 messages · lily li, Rui Barradas
Hello,
Maybe something like this?
Note that you *never* need to set header = FALSE, it already is the
default of read.table. You would have to with read.csv.
old_dir <- setwd("~/directory")
f1990 <- list.files(pattern = "^199.*\\.txt$")
r1990 <- lapply(f1990, read.table)
setwd(old_dir)
Hope this helps,
Rui Barradas
?s 08:30 de 25/03/2019, lily li escreveu:
Hi R users,
I have multiple dataframes in a directory, and with the same postfix
".txt". Each dataframe is a gridded file with just values, but each value
represents one grid cell. There are many years, and each year has 12
months, so there are many such files. For each year, I want to read the
corresponding year's files and add the values of the dataframes, but I
don't know how to do that. For example, the file names are
1990_file01.txt
1990_file02.txt
...
1990_file12.txt
1991_file01.txt
...
1991_file12.txt
...
And my code is like this:
f1990 = list.files("~/directory", pattern = "^1990(.*).txt$")
for(i in 1:length(f1990)){
r1990 = read.table(paste('~/directory',f1990[i],sep='/'),head=F)
}
Could you provide some help on this? Thanks very much.
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Sorry, I forgot to ask something. When you say you want to add the df's values, what exactly do you mean? All of the values, by row, by column, what? Rui Barradas ?s 10:20 de 25/03/2019, Rui Barradas escreveu:
Hello,
Maybe something like this?
Note that you *never* need to set header = FALSE, it already is the
default of read.table. You would have to with read.csv.
old_dir <- setwd("~/directory")
f1990 <- list.files(pattern = "^199.*\\.txt$")
r1990 <- lapply(f1990, read.table)
setwd(old_dir)
Hope this helps,
Rui Barradas
?s 08:30 de 25/03/2019, lily li escreveu:
Hi R users,
I have multiple dataframes in a directory, and with the same postfix
".txt". Each dataframe is a gridded file with just values, but each value
represents one grid cell. There are many years, and each year has 12
months, so there are many such files. For each year, I want to read the
corresponding year's files and add the values of the dataframes, but I
don't know how to do that. For example, the file names are
1990_file01.txt
1990_file02.txt
...
1990_file12.txt
1991_file01.txt
...
1991_file12.txt
...
And my code is like this:
f1990 = list.files("~/directory", pattern = "^1990(.*).txt$")
for(i in 1:length(f1990)){
r1990 = read.table(paste('~/directory',f1990[i],sep='/'),head=F)
}
Could you provide some help on this? Thanks very much.
????[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Each dataframe is a gridded file, so I want to add the values at corresponding grid cells from all the dataframes.
On Mon, Mar 25, 2019 at 6:22 PM Rui Barradas <ruipbarradas at sapo.pt> wrote:
Sorry, I forgot to ask something. When you say you want to add the df's values, what exactly do you mean? All of the values, by row, by column, what? Rui Barradas ?s 10:20 de 25/03/2019, Rui Barradas escreveu:
Hello,
Maybe something like this?
Note that you *never* need to set header = FALSE, it already is the
default of read.table. You would have to with read.csv.
old_dir <- setwd("~/directory")
f1990 <- list.files(pattern = "^199.*\\.txt$")
r1990 <- lapply(f1990, read.table)
setwd(old_dir)
Hope this helps,
Rui Barradas
?s 08:30 de 25/03/2019, lily li escreveu:
Hi R users, I have multiple dataframes in a directory, and with the same postfix ".txt". Each dataframe is a gridded file with just values, but each
value
represents one grid cell. There are many years, and each year has 12
months, so there are many such files. For each year, I want to read the
corresponding year's files and add the values of the dataframes, but I
don't know how to do that. For example, the file names are
1990_file01.txt
1990_file02.txt
...
1990_file12.txt
1991_file01.txt
...
1991_file12.txt
...
And my code is like this:
f1990 = list.files("~/directory", pattern = "^1990(.*).txt$")
for(i in 1:length(f1990)){
r1990 = read.table(paste('~/directory',f1990[i],sep='/'),head=F)
}
Could you provide some help on this? Thanks very much.
[[alternative HTML version deleted]]
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
______________________________________________ R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.