1

I have a function which I need to extract a column, when I use dataframe$column1[i], the function returns an error, but when I use dataframe[, column1][i], it will work. Why can't I just use dataframe$column vs dataframe[, column]???

function1 <- function(data, x, y) {

        data <- arrange(data, x)

        ylist <- NULL

        for(i in 1:nrow(data)){
                x0 <- data[, x][i] 
                data$d0 <- abs(data[,x] - x0)
                yi <- data %>% arrange(d0) %>% select(y) %>% unlist() %>% mean()
                ylist <- c(ylist, yi)

        }
        return(ylist)
}

If I wrote the function this way below, it'll return an error: Error in $<-.data.frame(*tmp*, "d0", value = numeric(0)) : replacement has 0 rows, data has 500

function1 <- function(data, x, y) {
    
            data <- arrange(data, x)
    
            ylist <- NULL
    
            for(i in 1:nrow(data)){
                    x0 <- data$x[i] 
                    data$d0 <- abs(data$x - x0)
                    yi <- data %>% arrange(d0) %>% select(y) %>% unlist() %>% mean()
                    ylist <- c(ylist, yi)
    
            }
            return(ylist)
    }
mnist
  • 6,571
  • 1
  • 18
  • 41
Jake Parker
  • 241
  • 1
  • 7
  • 1
    Could you provide some data so that it's possible to reproduce it? Easy way to do that is `dput()`. – Marco_CH Dec 12 '21 at 18:54

1 Answers1

0

From the help page of help("$"):

Both [[ and $ select a single element of the list. The main difference is that $ does not allow computed indices, whereas [[ does. x$name is equivalent to x[["name", exact = FALSE]]. Also, the partial matching behavior of [[ can be controlled using the exact argument.

To answer your question: it works this way because it has been designed to work this way. The $ takes the unquoted column name whereas [[/[ do take quoted column name(s). Different operators for different purposes.

mnist
  • 6,571
  • 1
  • 18
  • 41