[英]R: How to recode binary factor levels (0=0, 1=1) in BASE R
當我讀入 csv 文件時,有一個二進制目標。
as.factor 向后分配 0 和 1(索引方式)。
我想要 0='0' 和 1 ='1'
我在汽車 package(或者可能是 dplyr)中找到了 function 重新編碼,在 Tidyverse 中找到了另一個。 這兩個包都與其他包沖突並破壞了我的 R 代碼。
如何重新編碼 BASE r 中的二進制因子水平?
謝謝你的幫助。
我們可以在factor
中指定levels
和相應的labels
。
df1$col1 <- factor(df1$col1, levels = c(0, 1), labels = c('0', '1'))
這樣就可以改成任意順序了
factor(df1$col1, levels = c(0, 1), labels = c('1', '0'))
或者
factor(df1$col1, levels = c(0, 1), labels = c('2', '1'))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.