You're making two mistakes
Your file is not a .csv
- it's plaintext delimited by spaces, rather than commas. For this reason, read.csv
is returning a column vector of massive strings.
Your syntax is wrong inside mosaic::fav_stats
- you should be doing ALLBUS2018$ep01
, rather than ~ep01, data = ALLSBUS2018
, which is interpreted as fav_stats(x = ~ep01, data = ALLBUS2018)
. In this case, x
is the wrong type (a formula object) and data is passed as an additional argument via ...
and subsequently ignored. Check the help via ?mosaic::favstats
for more info on this.
This code should work
The names in your file are hard to read through the default read.table
methods, so I've done that in a separate step.
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
Created on 2021-01-23 by the reprex package (v0.3.0)