R的新手并不能解决这个问题。我有一个字符向量,将它放入data.frame并将它们更改为“factor”:
> name <- c("Ann","Bob", "Carl", "Dan","Ed")
> class(name)
[1] "character" # Expected this.
> wt <- c(123,234,222,199,201)
> class(wt)
[1] "numeric" # Expected this.
> a <- data.frame(name, wt)
> class(a$wt)
[1] "numeric" # Expected this.
> class(a$name)
[1] "factor" # ???
我不确定为什么会这样。
答案 0 :(得分:4)
正如评论中所述,在创建stringsAsFactors = FALSE
时使用data.frame
:
str(data.frame(name, wt, stringsAsFactors = FALSE))
# 'data.frame': 5 obs. of 2 variables:
# $ name: chr "Ann" "Bob" "Carl" "Dan" ...
# $ wt : num 123 234 222 199 201
默认行为适用于stringsAsFactors = TRUE
。可以在启动时更改此默认行为,但您可能不希望这样做是为了与其他人的脚本兼容。
基于data.frame
构建的其他一些包具有不同的默认行为。例如,考虑&#34; data.table&#34;中的data.table
。来自&#34; dplyr&#34;的包或data_frame
包:
library(data.table)
str(data.table(name, wt))
# Classes ‘data.table’ and 'data.frame': 5 obs. of 2 variables:
# $ name: chr "Ann" "Bob" "Carl" "Dan" ...
# $ wt : num 123 234 222 199 201
# - attr(*, ".internal.selfref")=<externalptr>
library(dplyr)
str(data_frame(name, wt))
# Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 5 obs. of 2 variables:
# $ name: chr "Ann" "Bob" "Carl" "Dan" ...
# $ wt : num 123 234 222 199 201