【发布时间】:2019-03-28 12:28:48
【问题描述】:
我有多个数据框。这些数据框中的列名具有特定的前缀。我想从这些数据帧中创建一个列表,以通过它们的前缀存储它们。
对于这个例子,我有两个数据框。每个都有两个不同的前缀。
dput(head(FEB_gems)) 输出:
structure(list(GAME1_Class = structure(c(2L, 1L, 5L, 4L, 3L), .Label = c("fighter",
"paladin", "rouge", "sorcerer", "wizard"), class = "factor"),
GAME1_Race = structure(c(3L, 1L, 4L, 3L, 2L), .Label = c("elf",
"gnome", "human", "orc"), class = "factor"), GAME1_Alignment = structure(c(4L,
2L, 1L, 5L, 3L), .Label = c("CE", "CG", "LG", "NE", "NN"), class = "factor"),
GAME1_Level = c(6, 7, 6, 7, 7), GAME1_Alive = structure(c(1L,
1L, 1L, 1L, 1L), .Label = "y", class = "factor"), GAME2_Class = structure(c(3L,
5L, 2L, 4L, 1L), .Label = c("bard", "cleric", "fighter",
"monk", "wizard"), class = "factor"), GAME2_Race = structure(c(2L,
3L, 2L, 4L, 1L), .Label = c("dwarf", "elf", "half-elf", "human"
), class = "factor"), GAME2_Alignment = structure(c(4L, 2L,
1L, 5L, 3L), .Label = c("CE", "CG", "LG", "NE", "NN"), class = "factor"),
GAME2_Level = c(5, 5, 5, 5, 5), GAME2_Alive = structure(c(1L,
2L, 2L, 2L, 2L), .Label = c("n", "y"), class = "factor")), row.names = c(NA,
5L), class = "data.frame")
dput(head(MAR_gems)) 输出:
structure(list(GAME3_Class = structure(c(2L, 1L, 5L, 4L, 3L), .Label = c("barbarian",
"cleric", "monk", "ranger", "warlock"), class = "factor"), GAME3_Race = structure(c(2L,
3L, 2L, 4L, 1L), .Label = c("dwarf", "elf", "half-elf", "human"
), class = "factor"), GAME3_Alignment = structure(c(2L, 2L, 1L,
3L, 2L), .Label = c("CE", "LG", "LN"), class = "factor"), GAME3_Level = c(1,
1, 1, 1, 1), GAME3_Alive = structure(c(2L, 2L, 2L, 1L, 2L), .Label = c("n",
"y"), class = "factor"), GAME4_Class = structure(c(2L, 1L, 5L,
4L, 3L), .Label = c("fighter", "paladin", "rouge", "sorcerer",
"wizard"), class = "factor"), GAME4_Race = structure(c(2L, 3L,
2L, 4L, 1L), .Label = c("dwarf", "elf", "half-elf", "human"), class = "factor"),
GAME4_Alignment = structure(c(1L, 2L, 1L, 4L, 3L), .Label = c("CE",
"CG", "LG", "LN"), class = "factor"), GAME4_Level = c(5,
5, 5, 5, 5), GAME4_Alive = structure(c(1L, 2L, 2L, 2L, 2L
), .Label = c("n", "y"), class = "factor")), row.names = c(NA,
5L), class = "data.frame")
对此进行了尝试,包括:
分离游戏信息
CharecterInfo <- function(df){
names(df) -> rons
gsub(x=names(df), pattern = '_.*', replacement = '') -> subn
subn[! duplicated(subn)] -> dupn
return(dupn)
}
CharecterInfo(FEB_games) -> FCharInfo
CharecterInfo(MAR_games) -> MCharInfo
然后尝试通过特定前缀分隔数据帧。
for (i in FCharInfo) {
assign(i, FCharInfo[, grep(paste0(i, '\\.'), colnames(FCharInfo), ignore.case = T)])
}
这不起作用,因为我的尺寸不正确,但只会制作许多数据框而不是制作列表。
我也用 R 中的 Map 函数做了一些尝试,但没有成功。
我的理想输出是:
1) 包含“GAME1_”、“GAME_2”、“GAME_3”和“GAME_4”数据作为单独数据帧的列表。
2)最好在函数中,因为我的真实数据比这里显示的数据大得多。
尝试说明理想列表的样子(我知道缩进/灰色框仅用于代码,但我不知道如何在 stackoverflow 上说明列表):
GameInfo
Game1_
GAME1_Class
GAME1_Race
GAME1_Alignment
GAME1_Level
GAME1_Alive
Game2_
GAME2_Class
GAME2_Race
GAME2_Alignment
GAME2_Level
GAME2_Alive
Game3_
GAME3_Class
GAME3_Race
GAME3_Alignment
GAME3_Level
GAME3_Alive
Game4_
GAME4_Class
GAME4_Race
GAME4_Alignment
GAME4_Level
GAME4_Alive
我的前缀有点复杂,可能包括:
GAME_1.Class
GAME_10.Class
GAME_100.Class
我试过(根据 Julian_hn 的回答)
Gems <- list(FEB_gems = FEB_games, MAR_gems = MAR_games)
Gems.split <- lapply(Gems, function(df)
{
Games <- unique(str_extract(names(df),"[:alnum:]+..."))
List <- lapply(Games,function(name){return(df[,grep(name,names(df))])})
names(List) <- Games
return(List)
})
但这并不能区分 1、100 或 1000。我可以在 '.' 之后分隔前缀吗?象征?
【问题讨论】: