将多个txt文件加载到单个数据帧,并将名称保留为R中的列 [英] Load multiple txt files to a single data frame and retain name as a column in R
本文介绍了将多个txt文件加载到单个数据帧,并将名称保留为R中的列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是R的新手,我正在尝试加载100个左右的名称,频率和性别的txt文件到一个单一的数据框架。这些文件都是名字yob1990.txt等。
I'm new to R and I'm trying to load 100 or so txt files with three columns Name, Frequency and Gender into a single data frame. The files are all name "yob1990.txt" etc.
我可以把所有的文件都存入R中,但我不知道如何添加一年的列你可以帮你吗?
I can get all the files into R but I've no idea how to add a column with just the year in it.. can anyone help please?
推荐答案
你也可以使用 fread
和 rbindlist
从 data.table
。如果文件在工作目录中,
You could also use fread
and rbindlist
from data.table
. If the files are in the working directory,
f1 <- list.files(pattern="^yob.*\\.txt")
f1 #created 3 files
#[1] "yob1990.txt" "yob1991.txt" "yob1992.txt"
library(data.table)
library(stringr)
year <- as.numeric(str_extract(f1, perl("[0-9]+(?=\\.txt)")))
res <- rbindlist(Map(`cbind`, lapply(f1, fread), year=year))
head(res)
# Name Frequency Gender year
#1: Sam 24 Male 1990
#2: Gobi 22 Male 1990
#3: Rose 44 Female 1990
#4: Anita 35 Female 1990
#5: John 44 Male 1991
#6: Sofia 52 Female 1991
或者你可以使用 notest
从 tidyr
devtools::install_github("hadley/tidyr")
library(tidyr)
res1 <- unnest(setNames(lapply(f1, fread), year), year)
head(res1)
# year Name Frequency Gender
#1 1990 Sam 24 Male
#2 1990 Gobi 22 Male
#3 1990 Rose 44 Female
#4 1990 Anita 35 Female
#5 1991 John 44 Male
#6 1991 Sofia 52 Female
这篇关于将多个txt文件加载到单个数据帧,并将名称保留为R中的列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文