Мы можем пройти по list
и получить среднее значение
df$avg <- sapply(s, function(x) mean(as.numeric(x)))
df$avg
#[1] 3.333333 1.500000 5.000000 2.000000
или используя tidyverse
library(tidyverse)
df %>%
separate_rows(counts, sep = ",", convert = TRUE) %>%
group_by(gene) %>%
summarise(avg = mean(counts), counts = toString(counts))
# A tibble: 4 x 3
# gene avg counts
# <chr> <dbl> <chr>
#1 a 3.33 1, 4, 5
#2 b 1.5 2, 1
#3 c 5 9, 2, 4, 5
#4 d 2 1, 2, 3
data
df <- structure(list(gene = c("a", "b", "c", "d"), counts = c("1,4,5",
"2,1", "9,2,4,5", "1,2,3")), class = "data.frame", row.names = c(NA,
-4L))
s <- strsplit(df$counts,split=",")