gpt4 book ai didi

r - 基于另一个数据框在数据框中扩展行和添加列

转载 作者:行者123 更新时间:2023-12-02 08:08:49 25 4
gpt4 key购买 nike

概览

team.df 中的每一行都包含一个 NBA team . list.of.all.stars 中的每个数据框包含基于 all star players 数量的多行与每支 NBA 球队相关联。

使用 apply()函数系列,我如何扩展 team.df 中的行以增加每个团队的所有明星球员的数量组合 列表中的列.of.all.stars 到最终输出?

我也完全接受非apply() 方法,只是想举一个例子,我希望避免编写 for 循环。

下面是我想要的输出:

#   Team_Name Team_Location         Player Captain
# 1 Cavaliers Cleveland, OH LeBron James TRUE
# 2 Cavaliers Cleveland, OH Kevin Love FALSE
# 3 Warriors Oakland, CA Stephen Curry TRUE
# 4 Warriors Oakland, CA Kevin Durant FALSE
# 5 Warriors Oakland, CA Klay Thompson FALSE
# 6 Warriors Oakland, CA Draymond Green FALSE

可重现的例子

# create data frame 
# about team information
team.df <-
data.frame(
Team_Name = c( "Cavaliers", "Warriors" )
, Team_Location = c( "Cleveland, OH", "Oakland, CA")
, stringsAsFactors = FALSE
)

# create list about
# all stars on each team
list.of.all.stars <-
list(
data.frame(
Player = c( "LeBron James", "Kevin Love" )
, Captain = c( TRUE, FALSE )
, stringsAsFactors = FALSE
)
, data.frame(
Player = c( "Stephen Curry", "Kevin Durant"
, "Klay Thompson", "Draymond Green"
)
, Captain = c( TRUE, FALSE, FALSE, FALSE )
, stringsAsFactors = FALSE
)
)

非 apply() 族方法

# cbind each data frame within the list.of.all.stars
# to its corresponding row in team.df
team.and.all.stars.list.of.df <-
list(
cbind(
df[ 1, ]
, list.of.all.stars[[1]]
)
, cbind(
df[ 2, ]
, list.of.all.stars[[2]]
)
)
# Warning messages:
# 1: In data.frame(..., check.names = FALSE) :
# row names were found from a short variable and have been discarded
# 2: In data.frame(..., check.names = FALSE) :
# row names were found from a short variable and have been discarded

# collapse each list
# into data frame
final.df <-
data.frame(
do.call(
what = "rbind"
, args = team.and.all.stars.list.of.df
)
, stringsAsFactors = FALSE
)
# view final output
final.df
# Team_Name Team_Location Player Captain
# 1 Cavaliers Cleveland, OH LeBron James TRUE
# 2 Cavaliers Cleveland, OH Kevin Love FALSE
# 3 Warriors Oakland, CA Stephen Curry TRUE
# 4 Warriors Oakland, CA Kevin Durant FALSE
# 5 Warriors Oakland, CA Klay Thompson FALSE
# 6 Warriors Oakland, CA Draymond Green FALSE

# end of script #

mapply() 尝试失败

# Hoping to Apply A Function
# using a data frame and
# a list of data frames
mapply.method <-
mapply(
FUN = function( x, y )
cbind.data.frame(
x
, y
, stringsAsFactors = FALSE
)
, team.df
, list.of.all.stars
)

# view results
mapply.method
# Team_Name Team_Location
# x Character,2 Character,4
# Player Character,2 Character,4
# Captain Logical,2 Logical,4

# end of script #

最佳答案

关于 OP 在 Map/mapply 中使用“team.df”作为输入的方法 “team.df”是一个 data.frame,它是一个 列的列表。因此,基本输入是一列 vector。它循环遍历 vector 或列而不是整个数据集或行(基于所需的输出)。为了防止这种情况,如果我们用 list 包装,它是一个单独的单元,它循环到 'list.of.all.stars' 的每个 list 元素

do.call(rbind, Map(cbind, list(team.df), list.of.all.stars))

根据预期的输出,“team.df”的每一行都应具有“list.of.all.stars”的相应 list 元素。在这种情况下,按行拆分“team.df”并执行cbind

res <- do.call(rbind, Map(cbind,  split(team.df, seq_len(nrow(team.df))), list.of.all.stars))
row.names(res) <- NULL
res
# Team_Name Team_Location Player Captain
#1 Cavaliers Cleveland, OH LeBron James TRUE
#2 Cavaliers Cleveland, OH Kevin Love FALSE
#3 Warriors Oakland, CA Stephen Curry TRUE
#4 Warriors Oakland, CA Kevin Durant FALSE
#5 Warriors Oakland, CA Klay Thompson FALSE
#6 Warriors Oakland, CA Draymond Green FALSE

我们也可以在 tidyverse 中做到这一点。在按“team.df”中的所有列分组后,嵌套 以创建“data”的基本列表(长度为 2),将“data”分配给“list.of”。 mutate 中的 all.stars' 和 unnest list

library(tidyverse)
team.df %>%
group_by_all() %>%
nest %>%
mutate(data = list.of.all.stars) %>%
unnest
# A tibble: 6 x 4
# Team_Name Team_Location Player Captain
# <chr> <chr> <chr> <lgl>
# 1 Cavaliers Cleveland, OH LeBron James T
# 2 Cavaliers Cleveland, OH Kevin Love F
# 3 Warriors Oakland, CA Stephen Curry T
# 4 Warriors Oakland, CA Kevin Durant F
# 5 Warriors Oakland, CA Klay Thompson F
# 6 Warriors Oakland, CA Draymond Green F

关于r - 基于另一个数据框在数据框中扩展行和添加列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48958869/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com