标识tbl_df列类

时间:2016-07-01 14:21:40

标签: r dplyr data-manipulation r-haven

我正在将SAS数据集读入R. SAS将缺少的字符值存储为空引号,但幸运的是zap_empty()将这些值转换为NA。

我的数据集包含近400个变量,我宁愿不单独检查每个变量。我想创建一个循环来标识变量是否是一个字符,然后应用zap_empty()

read_sas()将数据导入tbl_df而不是data.frame。如果我首先将数据转换为data.frame,则以下循环有效。

x <- as.data.frame(mydf)
for (i in seq(ncol(x))) {
  if(is.character(x[,i])){
    x[,i] <- zap_empty(x[,i])
  }
}

我想了解如何使用tbl_df使用布尔测试来标识列类。下面提供了一个示例SAS数据集,使用read_sas()中的haven进行阅读。

> wolves <- read_sas('http://psych.colorado.edu/~carey/Courses/PSYC7291/DataSets/SAS/wolves.sas7bdat')
>
> # The first 3 variables are characters
> glimpse(wolves)
Observations: 25
Variables: 13
$ location (chr) "rm", "rm", "rm", "rm", "rm", "rm", "rm", "rm", "rm"...
$ wolf     (chr) "rmm1", "rmm2", "rmm3", "rmm4", "rmm5", "rmm6", "rm"...
$ sex      (chr) "m", "m", "m", "m", "m", "m", "f", "f", "f", "m", "m"...
$ x1       (dbl) 126, 128, 126, 125, 126, 128, 116, 120, 116, 117, 1...
$ x2       (dbl) 104, 111, 108, 109, 107, 110, 102, 103, 103, 99, 10...
$ x3       (dbl) 141, 151, 152, 141, 143, 143, 131, 130, 125, 134, 1...
$ x4       (dbl) 81.0, 80.4, 85.7, 83.1, 81.9, 80.6, 76.7, 75.1, 74....
$ x5       (dbl) 31.8, 33.8, 34.7, 34.0, 34.0, 33.0, 31.5, 30.2, 31....
$ x6       (dbl) 65.7, 69.8, 69.1, 68.0, 66.1, 65.0, 65.0, 63.8, 62....
$ x7       (dbl) 50.9, 52.7, 49.3, 48.2, 49.0, 46.4, 45.4, 44.4, 41....
$ x8       (dbl) 44.0, 43.2, 45.6, 43.8, 42.4, 40.2, 39.0, 41.1, 44....
$ x9       (dbl) 18.2, 18.5, 17.9, 18.4, 17.9, 18.2, 16.8, 16.9, 17....
$ subject  (dbl) 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, ...
>
> # But my loop cannot identify that
> for (i in 1:ncol(wolves)){
+   if (is.character(wolves[,i])){
+     print('bar')
+   } else {print('foo')}
+ }
[1] "foo"
[1] "foo"
[1] "foo"
[1] "foo"
[1] "foo"
[1] "foo"
[1] "foo"
[1] "foo"
[1] "foo"
[1] "foo"
[1] "foo"
[1] "foo"
[1] "foo"

当我使用$访问该列时,它被标识为字符,但在使用索引时则不会。

> class(wolves$sex)
[1] "character"
> class(wolves[,'sex'])
[1] "tbl_df"     "data.frame"

使用循环,如何识别tbl_df对象中的哪些列是字符变量?

从@Sumedh我现在可以确定哪些列是字符 这个错误是否意味着我不能在循环中使用zap_empty()

> for (i in seq(which(sapply(wolves, class) == 'character'))){
+   wolves[,i] <- zap_empty(wolves[,i])
+ }
 Show Traceback

 Rerun with Debug
 Error: is.character(x) is not TRUE 

1 个答案:

答案 0 :(得分:1)

您希望确保在字符向量而不是单列数据框上测试is.character(x)

您的is.character(x[,i])未正确检查字符,因为单个括号下标始终会返回相同类型的对象。 由于x是数据框,x[,i]也是数据框。要获取字符向量,我们使用[[1]]选择x[,i]单列数据框中的第一个向量。

x <- as.data.frame(mydf)
for (i in seq(ncol(x))) {
  if(is.character(x[,i][[1]])){
    x[,i] <- zap_empty(x[,i][[1]])
  }
}

这里的数据科学R书更好,更全面地解释了这一点:http://r4ds.had.co.nz/vectors.html#recursive-vectors-lists

没有循环的一种方法:

character_vars <- lapply(x, class) == "character"
x[, character_vars] <- lapply(x[, character_vars], zap_empty)