R 为什么会有";逻辑的;参数返回向量与TIBLES的不同输出
有人能告诉我为什么“逻辑”参数返回向量与TIBLES的不同输出:R 为什么会有";逻辑的;参数返回向量与TIBLES的不同输出,r,tibble,R,Tibble,有人能告诉我为什么“逻辑”参数返回向量与TIBLES的不同输出: 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=rn
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不同之处不在于向量与tibble,而在于向量与列表(tibble/dataframes是一种特殊的列表)
向量只能保存一个类的数据。因此,a
的所有值都变成了字符,这是最常见的类,但数据帧/tibble的情况并非如此,它们可以在不同的列中保存不同类的数据
a<- c(1,0,"t")
a
#[1] "1" "0" "t"
class(a)
#[1] "character"
sapply(df, class)
# a b c
# "numeric" "numeric" "character"
a