简体   繁体   中英

Converting all binary factor columns to numeric

I have the following dataframe of all factors:

       A         B         C          D
1      0         0         0          0
2      1         1         1          1
3      1         0         0          2
4      0         1         1          3
5      1         0         1          1
6      1         0         0          3

I want to convert all columns that are binary and a factor to numeric (which are columns A,B, and C). How can I do this?

I tried this as a start just for 1/2 of the logic..

df %>% dplyr::mutate(across(where(is.factor), numeric))

But i get this error:

Error: Problem with `mutate()` input `..1`. ℹ `..1 = across(where(is.factor), numeric)`. x invalid 'length' argument Run `rlang::last_error()` to see where the error occurred.

We need two conditions in where also, the numeric would be as.numeric

library(dplyr)
df <- df %>%
   mutate(across(where(~ is.factor(.) && 
        all(. %in% c(0, 1))), ~ as.numeric(as.character(.))))

-output

str(df)
'data.frame':   6 obs. of  4 variables:
 $ A: num  0 1 1 0 1 1
 $ B: num  0 1 0 1 0 0
 $ C: num  0 1 0 1 1 0
 $ D: Factor w/ 4 levels "0","1","2","3": 1 2 3 4 2 4

data

df <- structure(list(A = structure(c(1L, 2L, 2L, 1L, 2L, 2L), .Label = c("0", 
"1"), class = "factor"), B = structure(c(1L, 2L, 1L, 2L, 1L, 
1L), .Label = c("0", "1"), class = "factor"), C = structure(c(1L, 
2L, 1L, 2L, 2L, 1L), .Label = c("0", "1"), class = "factor"), 
    D = structure(c(1L, 2L, 3L, 4L, 2L, 4L), .Label = c("0", 
    "1", "2", "3"), class = "factor")), row.names = c("1", "2", 
"3", "4", "5", "6"), class = "data.frame")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM