我正在尝试合并以下内容。
BattingData$ped[BattingData$playerID == "rodrial01" & BattingData$yearID == "1998"] <- "Y"
BattingData$ped[BattingData$playerID == "rodrial01" & BattingData$yearID == "1999"] <- "Y"
BattingData$ped[BattingData$playerID == "rodrial01" & BattingData$yearID == "2000"] <- "Y"
BattingData$ped[BattingData$playerID == "rodrial01" & BattingData$yearID == "2001"] <- "Y"
BattingData$ped[BattingData$playerID == "rodrial01" & BattingData$yearID == "2002"] <- "Y"
BattingData$ped[BattingData$playerID == "rodrial01" & BattingData$yearID == "2003"] <- "Y"
BattingData$ped[BattingData$playerID == "rodrial01" & BattingData$yearID == "2010"] <- "Y"
BattingData$ped[BattingData$playerID == "rodrial01" & BattingData$yearID == "2011"] <- "Y"
BattingData$ped[BattingData$playerID == "rodrial01" & BattingData$yearID == "2012"] <- "Y"
我能够使标准部分起作用,但是,我每年必须做一行。有什么办法巩固并说如果玩家是这个人,那么将这些年中的ped列中的值更改为“ Y”?
答案 0 :(得分:2)
1)内:折叠为以下单个语句:
within(BattingData,
ped[playerID == "rodrial01" & (yearID %in% c(1998:2003, 2010:2012))] <- "Y")
2)变换/替换另一种方法是:
yrs <- c(1998:2003, 2010:2012)
transform(BattingData,
ped = replace(ped, playerID == "rodrial01" & (yearID %in% yrs), "Y"))
答案 1 :(得分:1)
您还可以将其转换为int并按以下方式使用> = / <= :
BattingData$ped[BattingData$playerID == "rodrial01" & (strtoi(BattingData$yearID) >= 1998 & strtoi(BattingData$yearID) <= 2012)] <- "Y"
我希望将整个数据框列更改为 int