Ich habe einen Datenrahmen (df) mit drei Spalten wie folgt:Bedingte Zählung in Datenrahmen
Struktur:
id id1 age
A1 a1 32
A1 a2 45
A1 a3 45
A1 a4 12
A2 b1 15
A2 b5 34
A2 b64 17
Erwartete Ausgabe:
id count count1
A1 4 1
A2 3 2
Logic :
- Spalte "count" ist die Anzahl der "id" wiederholt wird
- Spalte "count1" ist die Anzahl der Zeilen, in denen Alter weniger als 21
Aktuelle Code:
library(dplyr)
df_summarized <- df %>%
group_by(id) >%>
summarise(count = n(),count1 = count(age<21))
Problem:
Error: no applicable method for 'group_by_' applied to an object of class "logical"