1. The data
First of all, you don't need to load an entire package to get one of its data sets, data
has an argument package
.
data(econmath, package = 'wooldridge')
str(econmath)
#> 'data.frame': 856 obs. of 17 variables:
#> $ age : int 23 23 21 22 22 22 22 22 22 21 ...
#> $ work : num 15 0 25 30 25 0 20 20 28 22.5 ...
#> $ study : num 10 22.5 12 40 15 30 25 15 7 25 ...
#> $ econhs : int 0 1 0 0 1 0 1 0 0 0 ...
#> $ colgpa : num 3.49 2.1 3.09 2.68 3.75 ...
#> $ hsgpa : num 3.35 3.22 3.31 3.98 3.89 ...
#> $ acteng : int 24 23 21 31 28 25 15 28 28 18 ...
#> $ actmth : int 26 20 24 28 31 30 19 30 28 19 ...
#> $ act : int 27 24 21 31 32 28 18 32 30 17 ...
#> $ mathscr : int 10 9 8 10 8 10 9 9 6 9 ...
#> $ male : int 1 1 1 0 1 1 0 1 0 0 ...
#> $ calculus: int 1 0 1 1 1 1 1 1 0 1 ...
#> $ attexc : int 0 0 1 0 0 1 0 1 1 0 ...
#> $ attgood : int 0 0 0 1 1 0 1 0 0 1 ...
#> $ fathcoll: int 1 0 0 1 0 0 0 1 0 0 ...
#> $ mothcoll: int 1 1 1 1 1 1 0 1 1 0 ...
#> $ score : num 84.4 57.4 66.4 81.2 95.9 ...
Created on 2022-12-31 with reprex v2.0.2
2. A bug corrected.
There was a bug in the question's code, data$actmiss
and data$act
refer to a non-existing data set.
Also, in its first use, ifelse
can be replaced by as.integer
.
data(econmath, package = 'wooldridge')
econmath$actmiss <- as.integer(is.na(econmath$act))
econmath$act0 <- ifelse(econmath$actmiss == 1L, 0L, econmath$act)
Created on 2022-12-31 with reprex v2.0.2
3. The question.
The operator $
extracts a list member or data.frame column, use [
and the error is gone, see Difference between [
and [[
.
In the code below I create a vector of columns of interest in order to make the code more readable. Then format the summaries with stargazer
.
Aren't you looking for the second table? Instead of explicitly calling summary
on the objects, argument summary
gives the best output.
data(econmath, package = 'wooldridge')
econmath$actmiss <- as.integer(is.na(econmath$act))
econmath$act0 <- ifelse(econmath$actmiss == 1L, 0L, econmath$act)
cols <- c("act", "act0")
stargazer::stargazer(summary(econmath[cols]), type = "text")
#>
#> =========================
#> act act0 NA
#> -------------------------
#> 1 act Min. :13.00
#> 2 act 1st Qu.:21.00
#> 3 act Median :23.00
#> 4 act Mean :23.12
#> 5 act 3rd Qu.:25.00
#> 6 act Max. :33.00
#> 7 act NA's :42
#> 8 act0 Min. : 0.00
#> 9 act0 1st Qu.:20.00
#> 10 act0 Median :23.00
#> 11 act0 Mean :21.99
#> 12 act0 3rd Qu.:25.00
#> 13 act0 Max. :33.00
#> 14 act0
#> -------------------------
stargazer::stargazer(econmath[cols], summary = TRUE, type = "text")
#>
#> =====================================
#> Statistic N Mean St. Dev. Min Max
#> -------------------------------------
#> act 814 23.122 3.349 13 33
#> act0 856 21.987 5.970 0 33
#> -------------------------------------
Created on 2022-12-31 with reprex v2.0.2
Another two ways are
stargazer::stargazer(summary(econmath['act']),
summary(econmath['act0']),
type = "text")
#>
#> =======================
#> act NA NA.1
#> -----------------------
#> 1 act Min. :13.00
#> 2 act 1st Qu.:21.00
#> 3 act Median :23.00
#> 4 act Mean :23.12
#> 5 act 3rd Qu.:25.00
#> 6 act Max. :33.00
#> 7 act NA's :42
#> -----------------------
#>
#> =========================
#> act0 NA NA.1
#> -------------------------
#> 1 act0 Min. : 0.00
#> 2 act0 1st Qu.:20.00
#> 3 act0 Median :23.00
#> 4 act0 Mean :21.99
#> 5 act0 3rd Qu.:25.00
#> 6 act0 Max. :33.00
#> -------------------------
stargazer::stargazer(econmath['act'],
econmath['act0'],
summary = TRUE,
type = "text")
#>
#> =====================================
#> Statistic N Mean St. Dev. Min Max
#> -------------------------------------
#> act 814 23.122 3.349 13 33
#> -------------------------------------
#>
#> =====================================
#> Statistic N Mean St. Dev. Min Max
#> -------------------------------------
#> act0 856 21.987 5.970 0 33
#> -------------------------------------
Created on 2022-12-31 with reprex v2.0.2