0

我正在尝试让 Favstats 工作。我正在使用带有数字变量的“正常”数据集,这些变量已加载:

ALLBUS2018 <- read.csv("~/Desktop/ALLBUS2018.csv", sep="")

当我对变量之一使用 Favstats 时,会发生以下情况:

fav_stats(~ ep01, data = ALLBUS2018, na.rm = TRUE)
Fehler in fav_stats(~ep01, data = ALLBUS2018, na.rm = TRUE) : 
  Objekt 'pairlist' kann nicht nach 'double' umgewandelt werden
Zusätzlich: Warnmeldung:
In fav_stats(~ep01, data = ALLBUS2018, na.rm = TRUE) :
  Auto-converting formula to numeric.

我已经重新安装了数据集并完全删除了 R。我的一个朋友用相同的输入得到了正确的输出,而 R 中没有其他数据。我试过 as.numeric 和 sapply(ALLBUS2018, function(txt) eval(parse(text=txt))) 在这里你看到另一个错误消息我有

在这里您可以找到使用的数据: https ://www.dropbox.com/s/fa9hplvk2j6q1cl/ALLBUS2018.csv?dl=0

谢谢你的帮助!HS

4

1 回答 1

0

你犯了两个错误

  1. 您的文件不是.csv- 它是由空格而不是逗号分隔的纯文本。出于这个原因,read.csv返回大量字符串的列向量。

  2. 你的语法在里面是错误的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 日创建

于 2021-01-23T12:06:36.690 回答