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)