将列表列表变量转换为数据框中的多个变量

时间:2017-03-06 17:58:50

标签: r

我正在抓取一个网站API,我遇到了将列表列表形式的变量转换为数据框中的列的问题。

我的数据框中的最后一个变量是一个变量,其中包含许多列表。如何将这些扩展为更高级别数据框中的所有变量?

我理想的输出是一个包含以下列的数据框:

datepatchtournamentstageroundseriesgamemapurlteamis_winnerbansdf$drafts等。

我的上一栏(library(httr) url <- "https://api.masterleague.net/matches" data <- GET(url) data <- content(data) df <- data.frame(t(sapply(data[[4]],c))) )包含了我想要的大量数据。我不知道怎么把它从列表列表中删除。

感谢您提供的所有帮助,如果您需要任何澄清,请与我们联系!

df

如果您只想使用df <- structure(list(id = list(2394L), date = list("2017-03-05"), patch = list(18L), tournament = list(38L), stage = list(175L), round = list( "Grand Final"), series = list(850L), game = list(5L), map = list(6L), url = list("https://masterleague.net/match/2394/"), drafts = list(list(structure(list(team = 20L, is_winner = TRUE, bans = list(53L, 30L), picks = list(structure(list(hero = 50L,player = 438L), .Names = c("hero", "player")), structure(list( hero = 26L, player = 102L), .Names = c("hero", "player" )), structure(list(hero = 52L, player = 104L), .Names = c("hero", "player")), structure(list(hero = 23L, player = 103L), .Names = c("hero", "player")), structure(list(hero = 34L, player = 101L), .Names = c("hero", "player")))), .Names = c("team", "is_winner", "bans", "picks")), structure(list(team = 21L, is_winner = FALSE, bans = list(51L, 35L), picks = list(structure(list(hero = 32L, player = 107L), .Names = c("hero", "player")), structure(list( hero = 47L, player = 108L), .Names = c("hero", "player" )), structure(list(hero = 21L, player = 106L), .Names = c("hero", "player")), structure(list(hero = 48L, player = 110L), .Names = c("hero", "player")), structure(list(hero = 20L, player = 105L), .Names = c("hero", "player")))), .Names = c("team", "is_winner", "bans", "picks"))))), .Names = c("id", "date", "patch", "tournament", "stage", "round", "series", "game", "map", "url", "drafts"), row.names = 1L, class = "data.frame") 的示例行,请输出:

rbind.data.frame

我尝试使用do.call(rbind)sapplystruct Player { vec2 m_position; vec2 m_size; std::unique_ptr<b2Body> m_body; Player( const vec2 &pos_ = {0.f,0.f}, const vec2 &size_ = {1.f,1.f} ) : m_position(pos_), m_size(size_), m_body(nullptr) {;} }; // In your code... std::unique_ptr<Player> m_player{ new Player({0.f,0.f},{3.f,2.f}) }; // make_unique if in C++14 b2World *m_world = new b2World(b2Vec2(0.f, -9.81f)); // use smart ptr here too.. b2BodyDef bodyDefinition; b2PolygonShape bodyShape; b2FixtureDef bodyFixture; // PLAYER BODY DEFINITION bodyDefinition.type = b2_dynamicBody; // PLAYER SHAPE bodyShape.SetAsBox(m_player->m_size.x, m_player->m_size.y); // PLAYER FIXTURE bodyFixture.shape = &bodyShape; // PLAYER BIND TO THE WORLD m_player->m_body.reset( m_world->CreateBody(&bodyDefinition) ); // or you can use the b2Body::CreateBody factory function. Then use b2Body::DestroyBody to delete the object. m_player->m_body->SetActive(true); m_player->m_body->CreateFixture(&bodyFixture); 以及我在此处找到的其他方法时未成功。

1 个答案:

答案 0 :(得分:1)

我认为这可以满足您的需求:

# use lapply to make a list of the unlisted lists (try saying that 10 times fast)
# within your last column
drafts <- lapply(seq(nrow(df)), function(i) unlist(df$drafts[i]))

# collapse that list into a data frame with rows corresponding to the ones in df
drafts2 <- do.call(rbind, drafts)

# use cbind to append that new data frame to the original one
df2 <- cbind(df, drafts2)

或者,如果您想一次性完成:

cbind(df, do.call(rbind, lapply(seq(nrow(df)), function(i) unlist(df$drafts[i]))))
相关问题