Favstats (R-Mosaic) returns an error due to non numeric variables although variables are only numeric

I am trying to get Favstats to work. I am using a "normal" Dataset with numeric variables that I have loaded in with:

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

When I use Favstats on one of the variables the following happens:

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.

I have re-installed the Dataset and deleted R completly. A friend of mine gets a correct output with the same input and no other data in R. I have tried as.numeric and sapply(ALLBUS2018, function(txt) eval(parse(text=txt)))Here you see another Error Message I got

Here you can find the data used:

Thanks for your help! HS

6

1 Answer

You're making two mistakes

  1. 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.

  2. 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)

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like