r - 为什么“逻辑”参数为向量和小标题返回不同的输出
问题描述
有人能告诉我为什么“逻辑”参数为向量和小标题返回不同的输出:
a<-c(1,0,"t")
the_numeric<-vector("logical",length(a))
for (i in seq_along(a)) the_numeric[[i]] <- is.numeric(a[[i]])
the_numeric
[1] FALSE FALSE FALSE
df<-tibble::tibble(
a=rnorm(10),
b=rnorm(10),
c=sample(letters,10)
)
the_numeric<-vector("logical",length(df))
for (i in seq_along(df)) the_numeric[[i]] <- is.numeric(df[[i]])
the_numeric
[1] TRUE TRUE FALSE
解决方案
区别不在于向量与小标题之间,而是向量与列表之间的区别(小标题/数据框是一种特殊的列表)。
向量只能保存一类数据。因此,a
成为字符的所有值是最常见的类,但数据框/小标题并非如此,它们可以在不同的列中保存不同类的数据。
a<- c(1,0,"t")
a
#[1] "1" "0" "t"
class(a)
#[1] "character"
sapply(df, class)
# a b c
# "numeric" "numeric" "character"
推荐阅读
- python - 改进 OCR 结果自定义
- angularjs - WebDataRocks 数据透视表不适用于 angularjs rest api
- nextcloud - Nextcloud:用于高级权限的 Groupfolder OCC
- python - 合并txt文件中未闭合的引号
- jmeter - 如何将 Json 提取器中的变量添加到 JMeter 中的数组中?
- reactjs - 单击找到的所有元素 - React 测试库
- php - 如果当前时间在星期五晚上 9:00 到晚上 10:30 之间,则显示文本
- java - 超类的泛型
- android - 如果放置在任何 Activity 布局中,Android Webview 会崩溃(自 3 月 22 日起)
- html - Aframe)图像未加载黑屏