我有一个1314元素长的列表。每个元素都是一个由两行四列组成的数据框。
Game.ID Team Points Victory
1 201210300CLE CLE 94 0
2 201210300CLE WAS 84 0
我想使用lapply函数来比较每个游戏中每个团队的积分,并将胜利团队的胜利更改为1。
我试图使用此功能:
test_vic <- lapply(all_games, function(x) {if (x[1,3] > x[2,3]) {x[1,4] = 1}})
但它产生的结果是一个列表1314个元素长,只有游戏ID和一个1或一个null,一个la:
$`201306200MIA`
[1] 1
$`201306160SAS`
NULL
如何修复代码以使每个数据框保持其形状。 (我猜测解决null部分涉及if-else,但我需要弄清楚正确的语法。)
感谢。
答案 0 :(得分:4)
尝试
lapply(all_games, function(x) {x$Victory[which.max(x$Points)] <- 1; x})
或另一种选择是使用list
将data.table
转换为rbindlist
,然后进行转换
library(data.table)
rbindlist(all_games)[,Victory:= +(Points==max(Points)) ,Game.ID][]
all_games <- list(structure(list(Game.ID = c("201210300CLE",
"201210300CLE"
), Team = c("CLE", "WAS"), Points = c(94L, 84L), Victory = c(0L,
0L)), .Names = c("Game.ID", "Team", "Points", "Victory"),
class = "data.frame", row.names = c("1",
"2")), structure(list(Game.ID = c("201210300CME", "201210300CME"
), Team = c("CLE", "WAS"), Points = c(90, 92), Victory = c(0L,
0L)), .Names = c("Game.ID", "Team", "Points", "Victory"),
row.names = c("1", "2"), class = "data.frame"))
答案 1 :(得分:2)
您可以尝试dplyr
:
library(dplyr)
all_games %>%
bind_rows() %>%
group_by(Game.ID) %>%
mutate(Victory = row_number(Points)-1)
给出了:
#Source: local data frame [4 x 4]
#Groups: Game.ID
#
# Game.ID Team Points Victory
#1 201210300CLE CLE 94 1
#2 201210300CLE WAS 84 0
#3 201210300CME CLE 90 0
#4 201210300CME WAS 92 1