Back
I have a data which consists of two columns "Students" & "points"
x<-data.frame(Students=c("Student1","Student2","Student3","Student2","Student3","Student1"),points=c(5,10,15,2,6,4))Students points1 Student1 52 Student2 103 Student3 154 Student2 25 Student3 66 Student1 4
x<-data.frame(Students=c("Student1","Student2","Student3","Student2","Student3","Student1"),points=c(5,10,15,2,6,4))
Students points
1 Student1 5
2 Student2 10
3 Student3 15
4 Student2 2
5 Student3 6
6 Student1 4
I want to sort the data and find the sum of the points in this way:
Students x1 Student1 92 Student2 123 Student3 21
Students x
1 Student1 9
2 Student2 12
3 Student3 21
There are two methods by which you can add the variables and sort the data:
By using aggregate() function:
aggregate(x$points, by=list(Students=x$Student), FUN=sum)
By using tapply() function:
tapply(x$points, x$Students, FUN=sum)
31k questions
32.8k answers
501 comments
693 users