Hello I'm trying to figure out how to merge rows (and sum the columns) of a data frame by prefix matching:
Example dataframe:
set.seed(42) ## for sake of reproducibility
df <- data.frame(col1=c(sprintf("gene%s", 1:3), sprintf("protein%s", 1:5), sprintf("lipid%s", 1:3)),
counts=runif(11, min=10, max=70))
df
# col1 counts
# 1 gene1 64.88836
# 2 gene2 66.22452
# 3 gene3 27.16837
# 4 protein1 59.82686
# 5 protein2 48.50473
# 6 protein3 41.14576
# 7 protein4 54.19530
# 8 protein5 18.08000
# 9 lipid1 49.41954
# 10 lipid2 52.30389
# 11 lipid3 37.46451
So I want that all the rows that starts with "gene" are merged into a single row and the same with the protein and lipid rows.
Desired output:
col1 counts
gene 158.2813
lipid 139.1879
protein 221.7526