Convert dataframe column to 1 or 0 for “true”/“false” values and assign to dataframe

前端 未结 5 1869
遇见更好的自我
遇见更好的自我 2020-12-01 15:48

In the R cli I am able to do the following on a character column in a data frame:

> data.frame$column.name [data.frame$column.name == \"true\"] <- 1
&g         


        
5条回答
  •  广开言路
    2020-12-01 16:22

    Since you're dealing with values that are just supposed to be boolean anyway, just use == and convert the logical response to as.integer:

    df <- data.frame(col = c("true", "true", "false"))
    df
    #     col
    # 1  true
    # 2  true
    # 3 false
    df$col <- as.integer(df$col == "true")
    df
    #   col
    # 1   1
    # 2   1
    # 3   0
    

提交回复
热议问题