本文介绍了为什么将数据帧中的转换逻辑应用到 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 所做的事情的解决方法是

A workaround to what you are doing with apply would be

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

但请注意,这两者完全相同

But note that these are both exactly the same as

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

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

10-26 21:58