要素和标签上的小标题

时间:2019-07-16 22:02:44

标签: r csv

我正在尝试使用值标签处理数据。 我有简单的CSV:
“ var”
“ A1”
“ A2”
...

当我使用read.csv导入数据并使用factor()时,数据已正确存储。

test <- read.csv("test.csv", sep="", stringsAsFactors=FALSE)
test$new <- factor(test[, 1], levels=c("A1","A2","A3"),labels=c("yes", "no", "don't know"))

dput返回:

structure(list(Var = c("A1", "A2", "A2", "A3", "A1", "A2"), new = structure(c(1L, 
2L, 2L, 3L, 1L, 2L), .Label = c("yes", "no", "don't know"), class = "factor")), row.names = c(NA, 
-6L), class = "data.frame")

但是当我使用readr::read_csv()尝试相同操作时,我的Var列被重新编码为NA。

test2 <- read_csv("test.csv")
test2$new <- factor(test2[, 1], levels=c("A1","A2","A3"),labels=c("yes", "no", "don't know"))

dput()返回:

structure(list(Var = c("A1", "A2", "A2", "A3", "A1", "A2"), new = structure(c(NA_integer_, 
NA_integer_, NA_integer_, NA_integer_, NA_integer_, NA_integer_
), class = "factor", .Label = c("yes", "no", "don't know"))), row.names = c(NA, 
-6L), spec = structure(list(cols = list(Var = structure(list(), class = c("collector_character", 
"collector"))), default = structure(list(), class = c("collector_guess", 
"collector")), skip = 1), class = "col_spec"), class = c("spec_tbl_df", 
"tbl_df", "tbl", "data.frame"))

我也尝试了parse_factor()中的readr::read_csv函数,但是此函数不支持标签,因为它是用factor()完成的。 那么如何将factor()与tibble对象一起使用,或者是否有其他简单的解决方案来向tibble添加值标签?

1 个答案:

答案 0 :(得分:1)

如果您查看new df1列的实际结构,您会发现“级别”已经消失,只有“标签”保留了:

dput(df1)
structure(list(cc = c("A1", "A2", "A2", "A2", "A2", "A2", "A3", 
"A3", "A1", "A2", "A2"), new = structure(c(1L, 2L, 2L, 2L, 2L, 
2L, 3L, 3L, 1L, 2L, 2L), .Label = c("yes", "no", "don't know"
), class = "factor")), row.names = c(NA, -11L), class = "data.frame")

因此,使用factor函数来执行任何操作都取决于该第二列的原始字符值(无论是否是小标题),都将失败。在您强制将其更改为小标题之前,原始df1可能会失败。