R 将数据帧中的值替换为同一数据帧中另一列中的相应值
这是我的数据帧dfR 将数据帧中的值替换为同一数据帧中另一列中的相应值,r,database,dataframe,replace,data-science,R,Database,Dataframe,Replace,Data Science,这是我的数据帧df country cum_cases v_dt_date MX 3 16 MX 2 18 MX 6 32 如果列v_dt_date==16中的值,我想将cum_cases中的行值更改为10 我试过这样做:df$cum_cases[df$v_dt_date==16]这里有几个选项: # Direct assign
country cum_cases v_dt_date
MX 3 16
MX 2 18
MX 6 32
如果列v_dt_date==16
中的值,我想将cum_cases
中的行值更改为10
我试过这样做:
df$cum_cases[df$v_dt_date==16]这里有几个选项:
# 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))
#直接分配-美观高效
df$cum_cases[df$v_dt_date==16]您的方法应该有效。class(df$v_dtu日期)
返回什么?class(df$v_dtu日期)“numeric”
您接受的答案与您的方法相同。但是你说你的方法不起作用……怎么了?对不起,我纠正了我的错误。问题是我需要把v_dt_date列的编号放在“”之间,即df$cum_cases[df$v_dt_date==“16”]
# 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))