How can I assign a value using if-else conditions in R

后端 未结 2 1484
你的背包
你的背包 2020-11-30 15:39

I have this dataframe with a column a. I would like to add a different column \'b\' based on column \'a\'.

For: if a>=10, b=\'double\'. Otherwise b=\'sing         


        
2条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-30 16:04

    You can use ifelse to act on vectors with if statements.

    ifelse(a>=10, "double", "single")
    

    So your code could look like this

    mydata <- cbind(a, ifelse(a>10, "double", "single"))
    

    (Specified in comments below that if a=10, then "double")

提交回复
热议问题