R metacor package error: $ operator is invalid for atomic vectors

I'm using the metacor function from the meta package with RStudio 1.4.1103 to test a meta-analytic bivariate correlation between self-reported 'propensity to trust' and 'trust in one's teammates', yet encounter an issue with my dataframe. I've imported the data from SPSS with this syntax:

library(foreign)
df=read.spss("Model 1.sav", use.value.labels=F, to.data.frame=T)

Here are the key components of the dataframe for this query:

studyid <- c('study1','study2','study3','study4','study5','study6','study7','study8','study9','study10','study11','study12','study13')
n <- c(87.0,30.0,8.0,79.0,68.0,62.0,16.0,26.0,28.0,19.0,96.0,27.0,34.0)
correlation <- c(.110,.380,.610,.170,.190,.350,.510,.190,.620,.790,.310,.840,.200)
df <- data.frame(studyid,n,correlation)

When I run the main model...

library(meta)
model1 <- metacor(cor = correlation, 
                  n = n,
                  studlab = studyid,
                  data = df,
                  comb.fixed = FALSE,
                  comb.random = TRUE,
                  sm = "ZCOR",
                  method.tau = "ML")
summary(model1) 

...I receive this error "$ operator is invalid for atomic vectors". I've checked the dataframe is not atomic:

is.atomix(df)
[1] FALSE

I can access specific elements of the dataframe, such as the correlations, but am unable to execute the meta-analytic component:

df$correlation
[1] 0.11 0.38 0.61 0.17 0.19 0.35 0.51 0.19 0.62 0.79 0.31 0.84 0.20

Does anyone know solutions to resolve this error with metacor?

I ran your code as provided and didn't see the error, the functions seemed to execute.
Perhaps restart your session ?
Or check that your shared example is representative of your issue ?

I get an error but a different one:

Error in eval(mf[[match("cor", names(mf))]], data, enclos = sys.frame(sys.parent())) : 
  invalid 'envir' argument of type 'closure'

However when I use stringsAsFactors=F all works (apparently well):

studyid <- c('study1','study2','study3','study4','study5','study6','study7','study8','study9','study10','study11','study12','study13')
n <- c(87.0,30.0,8.0,79.0,68.0,62.0,16.0,26.0,28.0,19.0,96.0,27.0,34.0)
correlation <- c(.110,.380,.610,.170,.190,.350,.510,.190,.620,.790,.310,.840,.200)
df <- data.frame(studyid,n,correlation,stringsAsFactors = F)

library(meta)
#> Loading 'meta' package (version 4.18-2).
#> Type 'help(meta)' for a brief overview.
model1 <- metacor(cor = correlation, 
                  n = n,
                  studlab = studyid,
                  data = df,
                  comb.fixed = FALSE,
                  comb.random = TRUE,
                  sm = "ZCOR",
                  method.tau = "ML")
summary(model1) 
#> Number of studies combined: k = 13
#> 
#>                         COR           95%-CI    z  p-value
#> Random effects model 0.4068 [0.2476; 0.5446] 4.73 < 0.0001
#> 
#> Quantifying heterogeneity:
#>  tau^2 = 0.0731 [0.0262; 0.3058]; tau = 0.2704 [0.1619; 0.5530]
#>  I^2 = 71.1% [49.2%; 83.5%]; H = 1.86 [1.40; 2.46]
#> 
#> Test of heterogeneity:
#>      Q d.f.  p-value
#>  41.48   12 < 0.0001
#> 
#> Details on meta-analytical method:
#> - Inverse variance method
#> - Maximum-likelihood estimator for tau^2
#> - Q-profile method for confidence interval of tau^2 and tau
#> - Fisher's z transformation of correlations
Created on 2021-07-19 by the reprex package (v2.0.0)

I quickly scanned the foreign::read.spss function but I don't see an argument similar to stringsAsFactors.

Good spot, so probably the following would solve for DanielG


df$studyid <- as.character(df$studyid)

thanks HanOostdijk and nirgrahamuk for the speedy feedback. Just confirming that

df$studyid <- as.character(df$studyid)

solved the problem.

I also updated the meta package, which worked perfectly without the above solution.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.