为什么将数据帧中的转换逻辑应用于5个字符的字符串? [英] Why does apply convert logicals in data frames to strings of 5 characters?

查看:77
本文介绍了为什么将数据帧中的转换逻辑应用于5个字符的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个数据框:

mydf <- data.frame(colA = c(1,20), colB = c("a", "ab"), colC = c(T, F))

现在假设我想对数据帧的每一行应用一个函数.此函数使用列C的布尔值.使用apply时,每个非字符串都将转换为该列中存在的最大长度的字符串:

Now suppose I want to apply a function to each row on the data frame. This function uses the boolean value of column C. When using apply, every non-string is converted to a string of the maximum length present in the column:

> apply(mydf, 1, '[', 3)
[1] " TRUE" "FALSE"

字符串" TRUE"不再可解释为逻辑.

The string " TRUE" is no longer interpretable as a logical.

> ifelse(apply(mydf, 1, '[', 3), 1, 2)
[1] NA  2

我可以用gsub(" ", "", x)解决这个问题,但是我敢打赌,还有更好的方法.当apply可以直接将逻辑转换为字符串时,为什么会出现这种行为?是否存在不具有上述行为的apply类函数?

I could solve this with a gsub(" ", "", x), but I'd bet there is a better way. Why does apply have this behavior when it could just directly convert the logicals to strings? Is there an apply-like function which does not have the above behavior?

推荐答案

调用apply时,您的数据框已转换为字符矩阵.出现空格是因为每个元素都转换为列中最宽元素的宽度.

When you called apply, your data frame was converted to a character matrix. The spaces appear because each element is converted to the width of the widest element in the column.

您可以通过类似for循环的sapply调用

You can do it with a for loop-like sapply call

> ( s <- sapply(seq(nrow(mydf)), function(i) mydf[i, 3]) )
# [1]  TRUE FALSE
> class(s)
# [1] "logical"

解决方法apply将会是

> as.logical(gsub("\\s+", "", apply(mydf, 1, `[`, 3)))
# [1]  TRUE FALSE

但是请注意,它们都与

> mydf[,3]
# [1]  TRUE FALSE

这篇关于为什么将数据帧中的转换逻辑应用于5个字符的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆