-2

I am trying to run the code below on RStudio and keep getting errors consistently. I have tried to research on it and have changed the code so many times but could not get it to work. Please review the code below and shed some light on where the error is? I would like to thank you before hand

//CODE

Code and compiler error

//COMPILER ERROR

EDIT.

The code posted by the OP in comments is:

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")
AD.H
  • 1
  • 2
  • 5
    Welcome to Where Developers Learn, Share, & Build Careers! Images and screenshots can be a nice addition to a post, but please make sure the post is still clear and useful without them. **Don't post images of code or error messages.** Instead copy and paste or type the actual code/message into the post directly. – rsjaffe Aug 06 '18 at 04:03
  • See what `str(mydata)` tells you. If the variables are factors, you have a `read.csv` problem. Start by using argument `stringsAsFactors = FALSE` and then convert to numeric with something like `mydata[] <- lapply(mydata, as.numeric)`. – Rui Barradas Aug 06 '18 at 04:21
  • Or `mydata[columns] <- lapply(mydata[columns], as.numeric)`. Then check if `NA`'s were introduced. Maybe there are illegal (non numeric) characters in the file. – Rui Barradas Aug 06 '18 at 04:23
  • all the variables are num and int as follows 'data.frame': 400 obs. of 4 variables: $ admit: int 0 1 1 1 0 1 1 0 1 0 ... $ gre : int 380 660 800 640 520 760 560 400 540 700 ... $ gpa : num 3.61 3.67 4 3.19 2.93 3 2.98 3.08 3.39 3.92 ... $ rank : int 3 3 1 4 4 2 1 2 3 2 ... – AD.H Aug 06 '18 at 04:24
  • the original code fragment is as follows mydata <- read.csv("https://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) – AD.H Aug 06 '18 at 04:29
  • In the function never do `data$col`, always `data[[col]]`. Also, we don't have access to the website you read from. Can you post sample data? Please edit **the question** with the output of `dput(head(mydata, 20))`. – Rui Barradas Aug 06 '18 at 04:36

1 Answers1

0

As mentioned in the comments, you should use data[[col]] instead of 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

Created on 2018-08-06 by the reprex package (v0.2.0).

Ralf Stubner
  • 26,263
  • 3
  • 40
  • 75
  • @AD.H You are welcome. The conventional way to say "thank you" on stack overflow is to accept/up-vote answers. And please follow the advice in the comments in your next question. See also [ask] and http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Ralf Stubner Aug 06 '18 at 05:36