[英]Replace value in data frame with corresponding value from another column in same dataframe
This my data frame df这是我的数据框df
country cum_cases v_dt_date
MX 3 16
MX 2 18
MX 6 32
If the value in the column v_dt_date == 16
I want to change the value of the row in cum_cases
to 10.如果列
v_dt_date == 16
我想将cum_cases
中的行的值更改为 10。
I tried with this: df$cum_cases[df$v_dt_date == 16]<-10
But is not working.我试过这个:
df$cum_cases[df$v_dt_date == 16]<-10
但不起作用。
Desired output所需 output
country cum_cases v_dt_date
MX 10 16
MX 2 18
MX 6 32
Here are a couple options:这里有几个选项:
# Direct assignment - nice and efficient
df$cum_cases[df$v_dt_date == 16] <- 10
# ifelse - generalizes well to more complex cases,
# e.g., nested conditions, more complex results
df$cum_cases <- with(df, ifelse(v_tdt_date == 16, 10, cum_cases))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.