我有以下数据框:
head(df)
Sl.no Date Year Month Season concentration
1 1 1993-12-01 1993 Dec Winter 20.0
2 2 1994-01-01 1994 Jan Winter 21.0
3 3 1994-02-01 1994 Feb Winter 20.0
4 4 1994-03-01 1994 Mar Spring 21.5
5 5 1994-04-01 1994 Apr Spring 29.0
6 6 1994-05-01 1994 May Spring 40.0
我想在数据框中添加一列,以便所有Dec,Jan,Feb,Mar,Apr显示"低流量"剩下的几个月来展示"高流量"。
我尝试了以下内容:
df$hflf <- if (df$Month==c("Dec", "Jan", "Feb", "Mar", "Apr"))
{"low flow"} else {"high flow"}.
但它似乎不起作用。
我怎样才能做到这一点。感谢您的帮助
答案 0 :(得分:0)
正如@akrun在评论中所建议的,你可以这样做:
lowflow <- c("Dec", "Jan", "Feb", "Mar", "Apr")
df$hflf <- ifelse(df$Year %in% lowflow, "low flow", "high flow")
使用dplyr
包也可以:
df <- df %>% mutate(hflf2 = case_when(Year %in% lowflow~"low flow", TRUE~"high flow"))