Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何按特定前缀将多个数据帧转换为列表_R_List_Function_Dataframe - Fatal编程技术网

R 如何按特定前缀将多个数据帧转换为列表

R 如何按特定前缀将多个数据帧转换为列表,r,list,function,dataframe,R,List,Function,Dataframe,我有多个数据帧。这些数据帧中的列名具有特定的前缀。我想从这些数据帧中创建一个列表,以一种通过前缀存储它们的方式 对于这个例子,我有两个数据帧。每个都有两个不同的前缀 dputheadFEB_gems输出: structure(list(GAME1_Class = structure(c(2L, 1L, 5L, 4L, 3L), .Label = c("fighter", "paladin", "rouge", "sorcerer", "wizard"), class = "factor"),

我有多个数据帧。这些数据帧中的列名具有特定的前缀。我想从这些数据帧中创建一个列表,以一种通过前缀存储它们的方式

对于这个例子,我有两个数据帧。每个都有两个不同的前缀

dputheadFEB_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")
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")
dputheadMAR_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")
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
这不起作用,因为我的维度不正确,只会生成许多数据帧,而不是列表

我还尝试了R中的Map函数,但没有成功

我的理想输出是:

1包含“游戏1”、“游戏2”、“游戏3”和“游戏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
我试着根据朱利安的回答

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)
})  

但这并不能区分1100和1000。我能把前缀和“.”符号分开吗

如果您的二月宝石和三月宝石data.frames是以列表的形式构建的,那么这对我来说是可行的

library(stringr)
Gems <- list(FEB_gems=FEB_gems,MAR_gems=MAR_gems)
Gems.Split <- lapply(Gems,function(df)
{
  #old solution, requires GAME as name
  #Games <- unique(str_extract(names(df),"GAME[0-9]+"))

  #old solution 2: splits at "_"
  #Games <- unique(str_sub(str_extract(names(df),"[:alnum:]+_"),end=-2))

  #new solution: splits at "."
  Games <- unique(str_extract(names(df),"\\S+\\."))
  List <- lapply(Games,function(name){return(df[,grep(name,names(df),fixed=T)])})
  names(List) <- Games
  return(List)
})
另一种可能的嵌套tidyverse解决方案:

library(tidyverse)

t_tib <- function(df) {
  as_tibble(cbind(nms = names(df), t(df)))
}

GameInfo <- t_tib(FEB_gems) %>% 
  bind_rows(t_tib(MAR_gems)) %>% 
  gather(rowid, val, -nms) %>% 
  separate(nms, into = c("Game", "var")) %>% 
  spread(var, val) %>% 
  select(-rowid) %>%
  select(1, 4, 6, 2, 5, 3) %>% 
  nest(-Game)
根据Moody Mudskipper的建议删除绑定。\

这里有一种使用tidyverse的方法:


bind_cols假设两个数据帧具有相同数量的行,这可能与示例数据中的情况相同,但这是一个强有力的假设。谢谢,这很有效。但是如果有多个不同的前缀呢。例如,在二月份的游戏中,如果有“GAME1”、“GAME2”、“Session1”和“Session2”,这将名称拆分为uu,而不关心之前是什么,只要它是字母数字。我在问题的底部添加了一个新的部分,以显示我正在处理的前缀的复杂性。是否可以在“.”而不是“"处拆分。抱歉,我知道以前没有提供此信息。当然。您只需更改str_摘录中的正则表达式。我再次添加到我的帖子中。这里的输出将GAME_1.Class和GAME_10.Class分组在GAME_1中。列表是否有一种方法使正则表达式更加严格。所以只有GAME_10.Class,GAME_11.Class等不会出现在GAME_1.Class中?