使用 sapply 将列转换为字符

Converting columns to character with sapply

我有一个数据框test,其列是因子

class(test)
[1] "data.frame"

sapply(test, class)
  street     city    state 
"factor" "factor" "factor" 

如果我尝试使用 sapply() 将这些列转换为字符,则会出错,我不确定为什么

test <- as.data.frame(sapply(test, as.character))

sapply(test, class)
  street     city    state 
"factor" "factor" "factor" 

我希望输出是所有字符列。为什么列没有转换,如何将所有因子列转换为字符?

这里是测试数据:

> dput(test)
structure(list(street = structure(c(5L, 1L, 6L, 2L, 3L, 4L), .Label = c("12057 Wilshire Blvd", 
"15300 Sunset Boulevard", "17380 Sunset Blvd", "1898 Westwood Blvd.", 
"3006 Sepulveda Blvd.", "514 Palisades Drive"), class = "factor"), 
    city = structure(c(1L, 1L, 2L, 2L, 2L, 3L), .Label = c("Los Angeles", 
    "Pacific Palisades", "Westwood"), class = "factor"), state = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L), .Label = "CA", class = "factor")), .Names = c("street", 
"city", "state"), row.names = c(NA, -6L), class = "data.frame")

这应该可以解决问题,它将 as.character 函数应用于数据框的每一列。 apply 函数将 return 一个矩阵,所以它只需要通过用 as.data.frame

包装它来强制转换为数据帧
test <- as.data.frame(apply(test, 2, as.character), stringsAsFactors = FALSE)

试试这个:

test[] <- lapply(test, as.character)

或者这个:

test <- modifyList(test, lapply(test, as.character))

或者这个:

test <- replace(test, TRUE, lapply(test, as.character))

试试 mutate_if,这应该也会给你更多的控制权:

mutate_if(test, is.factor, as.character)