导入多个文件并提取r中的特定列
我有20个数据文件(.txt)。我的最终目标是从每20个文件中选择一个特定的列(比如V3),并创建一个新文件。 我试过了导入多个文件并提取r中的特定列,r,R,我有20个数据文件(.txt)。我的最终目标是从每20个文件中选择一个特定的列(比如V3),并创建一个新文件。 我试过了 temp我们可以使用freadfromdata.table中的select选项,只选择我们想要读取的特定列,而不是读取整个数据 library(data.table) library(purrr) library(dplyr) map(temp, fread, select = 'V3') %>% bind_cols 如果行数不同,则使用cbind.fil
temp我们可以使用fread
fromdata.table
中的select
选项,只选择我们想要读取的特定列,而不是读取整个数据
library(data.table)
library(purrr)
library(dplyr)
map(temp, fread, select = 'V3') %>%
bind_cols
如果行数不同,则使用cbind.fill
out <- map(temp, fread, select = 'V3')
do.call(rowr::cbind.fill, c(out, fill = NA))
out%
可存储%>%
readr::write_csv(,path=.x)))
temp可以说,在多个文件中rbind()
相同变量的行比cbind()
它们要好,特别是当文件的行数不同时,cbind()
会失败
在只需要组合多个文件中的一列的情况下,我们也可以使用unlist()
而不是rbind()
使用base R组合行的完整工作示例可以使用匿名函数lappy()
和unlist()
完成。我们将使用Alex Barradas在kaggle.com上的神奇宝贝统计数据库中的数据,在那里我将数据重组为6个CSV文件,前六代神奇宝贝各一个
download.file("https://raw.githubusercontent.com/lgreski/pokemonData/master/pokemonData.zip",
"pokemonData.zip",
method="wininet",mode="wb")
unzip("pokemonData.zip")
thePokemonFiles <- list.files("./pokemonData",
full.names=TRUE)
attackStats <- lapply(thePokemonFiles,function(x) {
# read data and subset to Attack stat using the extract operator [
read.csv(x)["Attack"]
})
# unlist to combine into a vector
attackStats <- unlist(attackStats)
# use the data in another R function
hist(attackStats)
download.file(“https://raw.githubusercontent.com/lgreski/pokemonData/master/pokemonData.zip",
“pokemonda.zip”,
方法=“wininet”,模式=“wb”)
解压(“pokemonda.zip”)
只有当每个文件的行数相同时,此选项才有效。所有文件具有相同行数的可能性有多大?
set.seed(24)
invisible(map(paste0('snp.blp', 1:3, '.csv'), ~
matrix(sample(1:10, 10 * 3, replace = TRUE), ncol = 3,
dimnames = list(NULL, paste0("V", 1:3))) %>%
as_tibble %>%
readr::write_csv(., path = .x)))
temp <- list.files(pattern='snp.blp')
download.file("https://raw.githubusercontent.com/lgreski/pokemonData/master/pokemonData.zip",
"pokemonData.zip",
method="wininet",mode="wb")
unzip("pokemonData.zip")
thePokemonFiles <- list.files("./pokemonData",
full.names=TRUE)
attackStats <- lapply(thePokemonFiles,function(x) {
# read data and subset to Attack stat using the extract operator [
read.csv(x)["Attack"]
})
# unlist to combine into a vector
attackStats <- unlist(attackStats)
# use the data in another R function
hist(attackStats)