为什么 "logical" 参数 returns 向量与 tibbles 的不同输出
Why the "logical" argument returns different outputs for vectors vs. tibbles
有人能告诉我为什么 "logical" 参数 returns 向量与 tibbles 的不同输出:
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
区别不在于向量与 tibbles 之间,而是向量与列表之间(tibble/dataframes 是特殊类型的列表)。
向量只能保存一个 class 的数据。因此,a
的所有值都成为最常见的 class 字符,但 dataframes/tibbles 的情况并非如此,它们可以将不同 class 的数据保存在不同的位置列。
a<- c(1,0,"t")
a
#[1] "1" "0" "t"
class(a)
#[1] "character"
sapply(df, class)
# a b c
# "numeric" "numeric" "character"
有人能告诉我为什么 "logical" 参数 returns 向量与 tibbles 的不同输出:
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
区别不在于向量与 tibbles 之间,而是向量与列表之间(tibble/dataframes 是特殊类型的列表)。
向量只能保存一个 class 的数据。因此,a
的所有值都成为最常见的 class 字符,但 dataframes/tibbles 的情况并非如此,它们可以将不同 class 的数据保存在不同的位置列。
a<- c(1,0,"t")
a
#[1] "1" "0" "t"
class(a)
#[1] "character"
sapply(df, class)
# a b c
# "numeric" "numeric" "character"