你犯了两个错误
您的文件不是.csv
- 它是由空格而不是逗号分隔的纯文本。出于这个原因,read.csv
返回大量字符串的列向量。
你的语法在里面是错误的mosaic::fav_stats
——你应该做ALLBUS2018$ep01
,而不是~ep01, data = ALLSBUS2018
,被解释为fav_stats(x = ~ep01, data = ALLBUS2018)
. 在这种情况下,x
是错误的类型(公式对象)并且数据作为附加参数通过传递...
,随后被忽略。通过查看帮助?mosaic::favstats
以获取更多信息。
这段代码应该可以工作
文件中的名称很难通过默认read.table
方法读取,因此我在单独的步骤中完成了该操作。
require("mosaic")
csv_file <- ('"V1" "V2" "V3" "V4" "V5" "V6" "V7" "V8" "V9" "V10"
"1" NA "za_nr" "doi" "version" "respid" "eastwest" "german" "ep01" "ep03" "ep04"
"2" 1 "5270" "doi:10.4232/1.13250" "2.0.0 (2019-03-26)" "1" "1" "1" "1" "2" "2"
"3" 2 "5270" "doi:10.4232/1.13250" "2.0.0 (2019-03-26)" "2" "2" "1" "2" "4" "3"
"4" 3 "5270" "doi:10.4232/1.13250" "2.0.0 (2019-03-26)" "3" "1" "1" "2" "2" "3"
"5" 4 "5270" "doi:10.4232/1.13250" "2.0.0 (2019-03-26)" "4" "2" "1" "2" "2" "3"
"6" 5 "5270" "doi:10.4232/1.13250" "2.0.0 (2019-03-26)" "5" "2" "1" "3" "2" "3"
"7" 6 "5270" "doi:10.4232/1.13250" "2.0.0 (2019-03-26)" "6" "1" "1" "1" "3" "3"
"8" 7 "5270" "doi:10.4232/1.13250" "2.0.0 (2019-03-26)" "7" "1" "1" "3" "2" "3"
"9" 8 "5270" "doi:10.4232/1.13250" "2.0.0 (2019-03-26)" "8" "1" "1" "2" "3" "3"
"10" 9 "5270" "doi:10.4232/1.13250" "2.0.0 (2019-03-26)" "9" "1" "1" "1" "2" "4"')
ALLBUS2018 <- read.table(text = csv_file, sep = " ") # <- for the purpose of this example
# ALLBUS2018 <- read.table(file = "ALLBUS2018.csv", sep = " ") <- what you should do
### Fix row & colnames
colnames(ALLBUS2018) <- ALLBUS2018[1,]
ALLBUS2018 <- ALLBUS2018[-1,]
rownames(ALLBUS2018) <- ALLBUS2018[,1]
ALLBUS2018 <- ALLBUS2018[,-1]
# This syntax is wrong:
try(mosaic::fav_stats(~ep01, data = ALLBUS2018, na.rm = TRUE))
#> Warning in mosaic::fav_stats(~ep01, data = ALLBUS2018, na.rm = TRUE): Auto-
#> converting formula to numeric.
#> Error in mosaic::fav_stats(~ep01, data = ALLBUS2018, na.rm = TRUE) :
#> 'language' object cannot be coerced to type 'double'
# This syntax is right:
mosaic::fav_stats(ALLBUS2018$ep01, na.rm = TRUE)
#> Warning in mosaic::fav_stats(ALLBUS2018$ep01, na.rm = TRUE): Auto-converting
#> character to numeric.
#> min Q1 median Q3 max mean sd n missing
#> 1 1 2 2 3 1.888889 0.781736 9 0
由reprex 包(v0.3.0)于 2021 年 1 月 23 日创建