我正在尝试在RStudio上运行以下代码,并始终保持错误状态。我尝试对其进行研究,并已多次更改代码,但无法使其正常工作。请查看下面的代码,并弄清错误在哪里?我想先谢谢你
// 代码
// 编译器错误
编辑。
OP在评论中发布的代码是:
mydata <- read.csv("stats.idre.ucla.edu/stat/data/binary.csv")
head(mydata)
columns <- c("gre", "gpa", "rank")
mysummary <- function(col, data) {
c( mean = mean(data$col),
sd = sd(data$col),
quantile(data$col, c(0.25, 0.75)),
median = median(data$col)
)
}
sapply(columns, mysummary, mydata)
dput(head(mydata, 20))
dput(head(mydata, 20))
structure(list(admit = c(0L, 1L, 1L, 1L, 0L, 1L, 1L, 0L, 1L,
0L, 0L, 0L, 1L, 0L, 1L, 0L, 0L, 0L, 0L, 1L), gre = c(380L, 660L,
800L, 640L, 520L, 760L, 560L, 400L, 540L, 700L, 800L, 440L, 760L,
700L, 700L, 480L, 780L, 360L, 800L, 540L), gpa = c(3.61, 3.67,
4, 3.19, 2.93, 3, 2.98, 3.08, 3.39, 3.92, 4, 3.22, 4, 3.08, 4,
3.44, 3.87, 2.56, 3.75, 3.81), rank = c(3L, 3L, 1L, 4L, 4L, 2L,
1L, 2L, 3L, 2L, 4L, 1L, 1L, 2L, 1L, 3L, 4L, 3L, 2L, 1L)), row.names = c(NA,
20L), class = "data.frame")
答案 0 :(得分:0)
如评论中所述,您应该使用data[[col]]
而不是data$col
:
mydata <- read.csv("https://stats.idre.ucla.edu/stat/data/binary.csv")
head(mydata)
#> admit gre gpa rank
#> 1 0 380 3.61 3
#> 2 1 660 3.67 3
#> 3 1 800 4.00 1
#> 4 1 640 3.19 4
#> 5 0 520 2.93 4
#> 6 1 760 3.00 2
columns <- c("gre", "gpa", "rank")
mysummary <- function(col, data) {
c( mean = mean(data[[col]]),
sd = sd(data[[col]]),
quantile(data[[col]], c(0.25, 0.75)),
median = median(data[[col]])
)
}
sapply(columns, mysummary, mydata)
#> gre gpa rank
#> mean 587.7000 3.3899000 2.4850000
#> sd 115.5165 0.3805668 0.9444602
#> 25% 520.0000 3.1300000 2.0000000
#> 75% 660.0000 3.6700000 3.0000000
#> median 580.0000 3.3950000 2.0000000
由reprex package(v0.2.0)于2018-08-06创建。