[英]how do I replace numeric codes in a data frame with value labels from a (data.frame) lookup table?
[英]How do I convert a numeric table of that contains percentages in a Data Frame in R to a numeric table with ones and zeros given a threshold
對不起,如果這已經在另一篇文章中了,但我在這里是全新的,並且是 R 的超級綠色。
所以我想要實現的是,表格中百分比高於或等於 15% 的所有值都應變為 1,低於 15% 的所有值都應變為 0
我在想 gsub 但“被替換”可以成為一個條件嗎?
感謝大家嘗試幫助我解決了我的問題: Replaceing values from a column using a condition in R
當然你的也會有幫助,但由於我的限制,我無法使用它。 謝謝大家的幫助!
你可以這樣做:
library(dplyr)
data %>%
across(everything(), ~ case_when(.x >= 15 ~ 1, TRUE ~ 0))
使用 dplyr:
library(dplyr)
df <- data.frame(perc = c(0.1, 0.4, 0.13, 0.8))
df %>%
mutate(perc = ifelse(perc < 0.15, 0, 1))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.