[英]Recode data in an R dataframe column
I have a df with two columns: data and position, where: data are goals scored (1-10) and position is the position played (goalie, defence, forward)我有一个 df 有两列:数据和位置,其中:数据是进球数(1-10),位置是打的位置(守门员,防守,前锋)
I want to add a new column to the df, where if the position is "forward", for the row in a new column to say "good", otherwise, if it's "goalie" or "defence", for the row in the new column to say "bad" eg.我想在 df 中添加一个新列,如果位置是“前进”,则新列中的行表示“好”,否则,如果是“守门员”或“防守”,则表示新列说“坏”,例如。
data数据 | position位置 | new.column新列 |
---|---|---|
5 5 | goalie守门员 | bad坏的 |
6 6 | forward向前 | good好的 |
9 9 | defence防御 | bad坏的 |
5 5 | forward向前 | good好的 |
library(tidyverse)
df <- data.frame(data = c(5, 6, 9, 5),
position = c('goalie', 'forward', 'defense', 'forward')) %>%
mutate(new.column = case_when(
position == 'forward' ~'good',
TRUE ~'bad'
))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.