我想在data.frame内的列表中进行映射,将行绑定并在一个步骤中重复列ID。
如果有任何功能或使用映射功能的特定方法,我可以手动完成每个部分,但不能一步一步完成。
数据头:
data <- structure(list(mecanicas = list(structure(list(name = c("Campaign / Battle Card Driven",
"Cooperative Play", "Grid Movement", "Hand Management", "Modular Board",
"Role Playing"), objecttype = c("property", "property", "property",
"property", "property", "property"), objectid = c("2018", "2023",
"2676", "2040", "2011", "2028"), primarylink = c(0L, 0L, 0L,
0L, 0L, 0L), itemstate = c("approved", "approved", "approved",
"approved", "approved", "approved"), href = c("/boardgamemechanic/2018/campaign-battle-card-driven",
"/boardgamemechanic/2023/cooperative-play", "/boardgamemechanic/2676/grid-movement",
"/boardgamemechanic/2040/hand-management", "/boardgamemechanic/2011/modular-board",
"/boardgamemechanic/2028/role-playing")), class = "data.frame", row.names = c(NA,
6L)), structure(list(name = c("Action Point Allowance System",
"Cooperative Play", "Hand Management", "Point to Point Movement",
"Set Collection", "Trading"), objecttype = c("property", "property",
"property", "property", "property", "property"), objectid = c("2001",
"2023", "2040", "2078", "2004", "2008"), primarylink = c(0L,
0L, 0L, 0L, 0L, 0L), itemstate = c("approved", "approved", "approved",
"approved", "approved", "approved"), href = c("/boardgamemechanic/2001/action-point-allowance-system",
"/boardgamemechanic/2023/cooperative-play", "/boardgamemechanic/2040/hand-management",
"/boardgamemechanic/2078/point-point-movement", "/boardgamemechanic/2004/set-collection",
"/boardgamemechanic/2008/trading")), class = "data.frame", row.names = c(NA,
6L)), structure(list(name = c("Action Point Allowance System",
"Auction/Bidding", "Card Drafting"), objecttype = c("property",
"property", "property"), objectid = c("2001", "2012", "2041"),
primarylink = c(0L, 0L, 0L), itemstate = c("approved", "approved",
"approved"), href = c("/boardgamemechanic/2001/action-point-allowance-system",
"/boardgamemechanic/2012/auctionbidding", "/boardgamemechanic/2041/card-drafting"
)), class = "data.frame", row.names = c(NA, 3L)), list()),
title = c("Gloomhaven", "Pandemic Legacy: Season 1", "Through the Ages: A New Story of Civilization",
"KLASK")), row.names = c(NA, -4L), class = c("tbl_df", "tbl",
"data.frame"))
结构:
mecanicas title
<list> <chr>
1 <data.frame [6 x 6]> Gloomhaven
2 <data.frame [6 x 6]> Pandemic Legacy: Season 1
3 <data.frame [3 x 6]> Through the Ages: A New Story of Civilization
4 <list [0]> KLASK
我要简化的方式:
library('tidyverse')
### map and bind the rows
mechanics_binded <- map_dfr(data$mecanicas, bind_rows)
### then count the mechanics for repetition
n_mecs <- lapply(data[['mecanicas']], nrow) %>% as.character() %>% as.numeric()
##(some lists can be empty, but none in the data sample)
n_mecs[is.na(n_mecs)] <- 0
titles <- rep(data$title, n_mecs)
mechanics_binded$titles <- titles
mechanics <- mechanics_binded [,c('name', 'jogos')]
mechanics
所需结果:
name title
1 Campaign / Battle Card Driven Gloomhaven
2 Cooperative Play Gloomhaven
3 Grid Movement Gloomhaven
4 Hand Management Gloomhaven
5 Modular Board Gloomhaven
6 Role Playing Gloomhaven
7 Action Point Allowance System Pandemic Legacy: Season 1
8 Cooperative Play Pandemic Legacy: Season 1
9 Hand Management Pandemic Legacy: Season 1
10 Point to Point Movement Pandemic Legacy: Season 1
编辑:mecanicas
列也可以是一个空列表,否则结构良好。
EDIT2:从edit1中添加了一种边缘情况,即列表为空(tidyverse解决方案情况中的错误)。另一个错误(data.table解决方案)在没有完整数据的情况下无法重现,因此我在这里通过保管箱链接进行共享。 https://www.dropbox.com/s/boh8k0epay4gedh/bgg_mechanics.RData?dl=0
答案 0 :(得分:2)
您要function nextCycle(){
if(interrumpirCiclo){
interrumpirCiclo = false;
}else{
if(!detenerIntervalo){
avanzar();
}
}
setTimeout(nextCycle, 3000);
}
setTimeout(nextCycle, 3000);
:
tidyr::unnest
答案 1 :(得分:2)
使用数据表,您可以lapply
mecanicas
上title
上方,它会为您重复标题。
library(data.table)
setDT(data)
data[, lapply(mecanicas, `[[`, 'name'), by = title]
# title V1
# 1: Gloomhaven Campaign / Battle Card Driven
# 2: Gloomhaven Cooperative Play
# 3: Gloomhaven Grid Movement
# 4: Gloomhaven Hand Management
# 5: Gloomhaven Modular Board
# 6: Gloomhaven Role Playing
# 7: Pandemic Legacy: Season 1 Action Point Allowance System
# 8: Pandemic Legacy: Season 1 Cooperative Play
# 9: Pandemic Legacy: Season 1 Hand Management
# 10: Pandemic Legacy: Season 1 Point to Point Movement
# 11: Pandemic Legacy: Season 1 Set Collection
# 12: Pandemic Legacy: Season 1 Trading
# 13: Through the Ages: A New Story of Civilization Action Point Allowance System
# 14: Through the Ages: A New Story of Civilization Auction/Bidding
# 15: Through the Ages: A New Story of Civilization Card Drafting
答案 2 :(得分:1)
如果我理解正确,data
由
mecanicas
和title
中的数据帧相同的元素数的字符向量mecanicas
另一种方法是使用rbindlist()
“展平”数据结构,即将片段组合成一个大数据帧。
library(data.table)
# combine pieces to large data frame, add id col
flat <- rbindlist(data$mecanicas, idcol = "title")
# replace number in id col by title from character vector
flat[, title := data$title[title]][]
# extract desired columns
flat[, .(name, title)]
name title 1: Campaign / Battle Card Driven Gloomhaven 2: Cooperative Play Gloomhaven 3: Grid Movement Gloomhaven 4: Hand Management Gloomhaven 5: Modular Board Gloomhaven 6: Role Playing Gloomhaven 7: Action Point Allowance System Pandemic Legacy: Season 1 8: Cooperative Play Pandemic Legacy: Season 1 9: Hand Management Pandemic Legacy: Season 1 10: Point to Point Movement Pandemic Legacy: Season 1 11: Set Collection Pandemic Legacy: Season 1 12: Trading Pandemic Legacy: Season 1 13: Action Point Allowance System Through the Ages: A New Story of Civilization 14: Auction/Bidding Through the Ages: A New Story of Civilization 15: Card Drafting Through the Ages: A New Story of Civilization
由于OP报告了生产数据集的一些错误,因此有一些检查可以验证上述假设是否成立:
library(magrittr)
# check that number of columns of data frames is consistent
stopifnot(lengths(data$mecanicas) %>% all(.[1] == .))
# or, without piping:
(tmp <- lengths(data$mecanicas))
stopifnot(all(tmp[1] == tmp))
# check that number of data frames and titles is consistent
stopifnot(length(data$mecanicas) == length(data$title))