Error in xj[i] : only 0's may be mixed with negative subscripts

Hi Everyone,
I wrote an R function, when I call the function, it gave me the following error message:

wtMedian <- function(data, n){
chr <- unique(data[, which(colnames(data)=="chromosome")])
df <- data.frame(Col1 = character(),
                 Col2 = integer(),
                 Col3 = integer(),
                 Col4 = character(),
                 Col5 = double(),
                 Col6 = double(),
                 Col7 = double(), stringsAsFactors = FALSE)
weighted_m <- c() # new vector to store weighted median
for (ch in chr){
   data1 <- data %>% filter(chromosome %in% ch) # subset certain chromosome
  # nch <- length(data1$log2) # the $ will cause error when calling the function
  nch <- length(data1[,which(colnames(data)=="log2")])
  
  for (i in c(1:n)){
    #m <- median(data1[c(1:(n+i-1)),6])
    dat <- data1[c(1:(n+i-1)), which(colnames(data1)=="log2")] # data set for calculating weighed median
    w <- data1[c(1:(n+i-1)), which(colnames(data1)=="weight")] # weight set for calculating weighted median
    m <- weightedMedian(dat,w) # calculate weighted median
    weighted_m <- append(weighted_m, m)
  }
  for (i in c((n+1):(nch-n))){
    dat <- data1[c((i-n):(i+n-1)), which(colnames(data1)=="log2")]
    w <- data1[c((i-n):(i+n-1)), which(colnames(data1)=="weight")]
    m <- weightedMedian(dat,w)
    weighted_m <- append(weighted_m, m)
  }
  for (i in c((nch-n+1):nch)){
    dat <- data1[c((i-n):nch), which(colnames(data1)=="log2")]
    w <- data1[c((i-n):nch), which(colnames(data1)=="weight")]
    m <- weightedMedian(dat,w)
    weighted_m <- append(weighted_m, m)
  }
  df <- rbind(df, data1)
}
df$log2 <- weighted_m #assign weighted median to original dataframe
return(df)
}
df <- wtMedian(data,25)

Question2:
when I use $ in the function, there will be an error when calling the function, for example, if I replaced


chr <- unique(data[, which(colnames(data)=="chromosome")])

with

chr <- unique(data$chromosome). # the error message will be :

Error: [: $ operator is invalid for atomic vectors].
I used r which() to avoid that error, but causing the first error as mentioned.

However, if I don't run the above code in a function, everything will be fine.

Can anyone help me with the troubleshooting and explain what cause these errors, and why this specifically occurred in functions?

Greatly appreciated your help.

Best,
Lc

Question 1: hard to say for sure without knowing what is in data (please consider making a reprex), but it comes from using [ to select:

# let's create an example vector x
x <- 10:15

# you can use positive indexes to select one or several values
x[c(2,4)]
#> [1] 11 13

# you can use negative values to exclude these (and keep everything else)
x[ - c(2,4)]
#> [1] 10 12 14 15

# mixing the two is meaningless: do you want to keep or exclude the ones that are not mentioned?
x[c( -1, 3)]
#> Error in x[c(-1, 3)] : only 0's may be mixed with negative subscripts

more explanations here

So in your function, somehow there is a combination of i and n where c((i-n):(i+n-1)) contains both positive and negative values, R can't guess what you want to do.

Question 2: looks like data is not a data.frame, according to the error message. It works for me:

wtMedian <- function(data){
  chr <- unique(data$chromosome)
  chr
}

my_data <- data.frame(chromosome = c("I","II"),
                       bla = 1:2)
wtMedian(my_data)
#> [1] "I"  "II"

Created on 2021-11-17 by the reprex package (v2.0.1)

Hi AlexisW,

Thank for the explanation. That helps a lot in understanding the error. My issue was solved by changing those for loops into if statements. The error disappears.

Thanks a lot.

Best,
LC

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