0

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: https://www.dropbox.com/s/fa9hplvk2j6q1cl/ALLBUS2018.csv?dl=0

Thanks for your help! HS

  • Same Problem with median: > median(~rechts, data=ALLBUS2018) Fehler in as.data.frame.default(x[[i]], optional = TRUE) : cannot coerce class ‘"function"’ to a data.frame – Hans Schmidt Jan 23 '21 at 10:34
  • Hi Hans. Welcome to SO. This question is difficult to answer because it does not feature a minimal, reproducible example. Please see this guide: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example and edit your question accordingly. – Captain Hat Jan 23 '21 at 10:52
  • R might be reading all your csv data as character data. You could try `readr::read_csv()` instead of `base::read.csv()` – Captain Hat Jan 23 '21 at 10:56
  • Sorry, I have added the data so that the example should be reproducable. – Hans Schmidt Jan 23 '21 at 11:34
  • Could you show me the full codeline to load the document with readr:read_csv() ? It does not seem to work for me. – Hans Schmidt Jan 23 '21 at 11:35
  • Hi Hans, turns out that wasn't your problem. Check the answer below – Captain Hat Jan 23 '21 at 12:08

1 Answers1

0

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)

Captain Hat
  • 2,444
  • 1
  • 14
  • 31
  • Hi Hans. I'm glad it helped. If this answered your question, could you accept it as the answer? This helps other users to find the answers they are looking for. Comments are generally used for further discussion – Captain Hat Jan 23 '21 at 13:15