troubles using strata in MacRStudio 2022.07.2+576

Hello everybody, I am new in RStudio.
When I was doing the example of Stratified sampling.
data=rbind(matrix(rep("nc",165),165,1,byrow=TRUE),matrix(rep("sc",70),70,1,byrow=TRUE))
data=cbind.data.frame(data,c(rep(1,100), rep(2,50), rep(3,15), rep(1,30),rep(2,40)),
1000*runif(235))
names(data)=c("state","region","income")
The following line doesn't worked:
s=strata(data,c("region","state"),size=c(10,5,10,4,6), method="srswor")
This is the error message:
Error in strata(data, c("region", "state"), size = c(10, 5, 10, 4, 6), : all arguments must be the same length

See the FAQ: How to do a minimal reproducible example reprex for beginners. It's great that you included most of what's required. Preferably it turns out to be something that can be cut-and-pasted to give the exact error message.

Here's my reprex. All I changed is data to dat because data(), like df() and others, are conflicting names. Sometimes a user assigned name takes precedence and other times it is the function. Whenever the cannot subset a closure message it's usually because of the naming issues.

library(sampling)
dat <- rbind(matrix(rep("nc", 165), 165, 1, byrow = TRUE), matrix(rep("sc", 70), 70, 1, byrow = TRUE))
dat <- cbind.data.frame(
  dat, c(rep(1, 100), rep(2, 50), rep(3, 15), rep(1, 30), rep(2, 40)),
  1000 * runif(235)
)
names(dat) <- c("state", "region", "income")

s <- strata(dat,c("region","state"),size=c(10,5,10,4,6), method="srswor")
head(s)
#>    region state ID_unit Prob Stratum
#> 6       1    nc       6  0.1       1
#> 14      1    nc      14  0.1       1
#> 29      1    nc      29  0.1       1
#> 31      1    nc      31  0.1       1
#> 65      1    nc      65  0.1       1
#> 68      1    nc      68  0.1       1

Thanks a lot, but even when I changed data to dat, I have the same message:
Error in strata(dat, c("region", "state"), size = c(10, 5, 10, 4, 6), :
all arguments must be the same length

Cut and paste my code and see if you get the same? And you are using strata from the {sampling} package, not some other version?

1 Like

I am really grateful, your solution was correct. Thanks.

1 Like

Is there a chance that after I closed and save the script, when I try to run it again. The script doesn't work?
Because that happened, even when I cut and copy your solution again.

With a fresh session (so that there is nothing left over to interfere from whatever I was doing before), it works identically.

library(sampling)
dat <- rbind(matrix(rep("nc", 165), 165, 1, byrow = TRUE), matrix(rep("sc", 70), 70, 1, byrow = TRUE))
dat <- cbind.data.frame(
  dat, c(rep(1, 100), rep(2, 50), rep(3, 15), rep(1, 30), rep(2, 40)),
  1000 * runif(235)
)
names(dat) <- c("state", "region", "income")

s <- strata(dat,c("region","state"),size=c(10,5,10,4,6), method="srswor")
head(s)
#>    region state ID_unit Prob Stratum
#> 21      1    nc      21  0.1       1
#> 37      1    nc      37  0.1       1
#> 41      1    nc      41  0.1       1
#> 45      1    nc      45  0.1       1
#> 49      1    nc      49  0.1       1
#> 64      1    nc      64  0.1       1


Unfortunatelly, it doesn't work for me.

From what I can make out from the screenshot, the code is the same as I am using but I don't get the error message on either Ubuntu 22.04 or macOS 13.0.1. I'm using RStudio 2022.07.2 Build 576. It also works from the R Console in Terminal. I'm not sure what the problem could be.

This topic was automatically closed 42 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.