In the following df, some cells in column contains value (numeric, logical, character) and some are empty. I want to assign value 1 in all the cells that are not empty and assign value 0 to all the empty cells and want to create newdf. Using this newdf I want to generate heatmap grouped by "citizen" type in y-axis. Any help?
structure(list(ID = c("ID123", "ID456", "ID523", "ID875", "ID782",
"ID572", "ID900"), Citizen = c("US", "CN", "MX", "US", "US",
"CA", "CA"), Ht = c("6", "NA", "5", "6", "5", NA, "6"), Wt = c("200",
"140", "160", NA, "NA", "175", NA), Age = c("NA", "45", NA, "32",
"60", "44", "30"), income = c("60", "50", "30", "20", "40", "NA",
"20"), sex = c("M", "F", "NA", NA, "M", "M", "F"), `Traffic vio` = c(TRUE,
FALSE, TRUE, FALSE, NA, TRUE, TRUE), Greets = c("Hello", "Bonjour",
"Hola", "Hi", "Hello", "Hello", "Bonjour")), row.names = c(NA,
-7L), class = c("tbl_df", "tbl", "data.frame"))
Try this dplyr
solution with across()
. You have different classes of variables so you can format all of them as character and then make the replacement. Next the code for the 0-1 replacement:
library(dplyr)
#Code
#First make variables in standard format
df %>%
mutate(across(Ht:Greets,~as.character(.))) %>%
#Now mutate to 0-1
mutate(across(Ht:Greets,~ifelse(is.na(.),0,1)))
Output:
# A tibble: 7 x 9
ID Citizen Ht Wt Age income sex `Traffic vio` Greets
<chr> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 ID123 US 1 1 1 1 1 1 1
2 ID456 CN 1 1 1 1 1 1 1
3 ID523 MX 1 1 0 1 1 1 1
4 ID875 US 1 0 1 1 0 1 1
5 ID782 US 1 1 1 1 1 0 1
6 ID572 CA 0 1 1 1 1 1 1
7 ID900 CA 1 0 1 1 1 1 1
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.