簡體   English   中英

使用來自 Tidyverse 的 R&readr:如何將數據幀作為因子和標簽導入

[英]Using R & readr from Tidyverse: How to Import dataframe as Factor and with Labels

我正在使用 Tidyverse 中的 R(-cran) 和readr庫。 我有一個來自 .csv 文件的特征(列)是因素,例如性別 = c(0,1)。

如何將數據導入為帶標簽的因子,使它們更有意義,例如:

df.csv = c("男",0,1,0,1,0)

df <- read_csv("df.csv", 
               col_types = cols(male = col_factor(levels = c("0","1"), 
                                                  labels = c("F","M")))

但是,我收到錯誤消息:

Error in col_factor(levels = c("0", "1"), labels = c("F", "M")) : 
unused argument (labels = c("F", "M"))

但是,我想要:0="F" 和 1="M"。

這是一種方法:

df <- read_csv("df.csv", 
               col_types = cols(male = col_factor(levels = c("0","1"))))
# Followed by
df$column = factor(df$column, labels=c("F","M"))

str(df)

tibble [3,656 × 1] (S3: tbl_df/tbl/data.frame)

$ 男性:因子 w/ 2 個級別“F”、“M”:2 1 2 1 1 1 1 1 2 2 ...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM