R语言 试图根据一列中的值更改另一列中的两个值

gkl3eglg  于 2023-05-26  发布在  其他
关注(0)|答案(2)|浏览(128)

我尝试根据Y列中的值将位置列中的“T”值更改为“LT”或“RT”,并将“G”值更改为“LG”或“RG”。例如,两个“T”值中具有两者之间的较高Y值的那个应该是“LT”,并且具有较低Y值的那个应该是“RT”。这同样适用于“G”位置。
以下是一个示例 Dataframe :

tracking_data <- data.frame(
  GameId = c("2017090700", "2017090700", "2017090700", "2017090700", "2017090700", "2017090700"),
  PlayId = c("20170907000118", "20170907000118", "20170907000118", "20170907000118", "20170907000118", "20170907000118"),
  Y = c(20.92333, 28.21333, 23.84333, 22.80333, 21.45333, 24.86333),
  Position = c("T", "T", "QB", "RB", "G", "G")
)

以下是我想要的:

tracking_data <- data.frame(
  GameId = c("2017090700", "2017090700", "2017090700", "2017090700", "2017090700", "2017090700"),
  PlayId = c("20170907000118", "20170907000118", "20170907000118", "20170907000118", "20170907000118", "20170907000118"),
  Y = c(20.92333, 28.21333, 23.84333, 22.80333, 21.45333, 24.86333),
  Position = c("RT", "LT", "QB", "RB", "RG", "LG")
)

我该怎么做呢?这只是一个例子,在这个数据集中还有成千上万的其他行。

izkcnapc

izkcnapc1#

我不知道这是否可以推广到你的真实的数据问题,但是如果你按GameIDPlayIDPosition分组,然后按Y的升序排列它们,你应该可以将R和L粘贴到值G和T上,并得到正确的答案。

library(dplyr)
tracking_data <- data.frame(
  GameId = c("2017090700", "2017090700", "2017090700", "2017090700", "2017090700", "2017090700"),
  PlayId = c("20170907000118", "20170907000118", "20170907000118", "20170907000118", "20170907000118", "20170907000118"),
  Y = c(20.92333, 28.21333, 23.84333, 22.80333, 21.45333, 24.86333),
  Position = c("T", "T", "QB", "RB", "G", "G")
)
  
tracking_data %>% 
  group_by(GameId, PlayId, Position) %>% 
  arrange(Y, .by_group=TRUE) %>% 
  mutate(Position = ifelse(Position %in%  c("G", "T"), paste0(c("R", "L"), Position), Position))
#> # A tibble: 6 × 4
#> # Groups:   GameId, PlayId, Position [6]
#>   GameId     PlayId             Y Position
#>   <chr>      <chr>          <dbl> <chr>   
#> 1 2017090700 20170907000118  21.5 RG      
#> 2 2017090700 20170907000118  24.9 LG      
#> 3 2017090700 20170907000118  23.8 QB      
#> 4 2017090700 20170907000118  22.8 RB      
#> 5 2017090700 20170907000118  20.9 RT      
#> 6 2017090700 20170907000118  28.2 LT

创建于2023-05-16带有reprex v2.0.2

u0njafvf

u0njafvf2#

只需对它们进行排序,并使用带有循环的矢量化替换

key <- with(tracking_data, paste(GameId, PlayId, Position, Y))
tracking_data <- tracking_data[order(key), ]
tracking_data[tracking_data$Position == 'T', 'Position'] <- c('RT', 'LT')
tracking_data[tracking_data$Position == 'G', 'Position'] <- c('RG', 'LG')

相关问题