'Sum with particular condition
I would like to know some information, it is possible make a group by with this particular condition:
- Column Sepal.length take the first value;
- Sum the column that began with Petal
- Not select Sepal.Width
I have try this but I doesn't work:
m <- iris
    
m %>% 
  group_by(Species) %>% 
  summarise(across(starts_with("Petal"), sum)) %>% 
  summarise(F = first(Sepal.length))  # first for each Species
Solution 1:[1]
Using data.table:
M = data.table(iris)
M[, 
  .(Petal.sum = sum(.SD), F = first(Sepal.Length)), 
  by = Species,
  .SDcols = patterns('^Petal')]
#       Species Petal.sum     F
#        <fctr>     <num> <num>
# 1:     setosa      85.4   5.1
# 2: versicolor     279.3   7.0
# 3:  virginica     378.9   6.3
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source | 
|---|---|
| Solution 1 | sindri_baldur | 
