I have a dataframe that has 100 subjects' data in it. I want to perform some analysis on each subjects data, individually. Because of the large size, its not feasible to manually select certain rows. Is there a way i can perform an analysis for each subject which outputs the data of interest to an array?
Example df:
subject <- c(1,1,1,2,2,2,3,3,3)
score <- c(c(10, 20, 30, 40, 50, 60, 70, 80, 90))
other_data <- c('x', 'y', 'z', 'x', 'y', 'z', 'x', 'y', 'z')
df <- data.frame(subject, score, other_data)
df
which prints:
subject score other_data
1 1 10 x
2 1 20 y
3 1 30 z
4 2 40 x
5 2 50 y
6 2 60 z
7 3 70 x
8 3 80 y
9 3 90 z
I have tried the following: I simply used 'sum' for an example. I want the sum of each of the 3 subjects scores' output to an array.
for (i in unique(subject)){
subject_score <- sum(score)
print(subject_score)
}
Which prints the total sum for all subjects 3 times, not the sum for each subject:
[1] 450
[1] 450
[1] 450
I would like to know which subject had which (sum) score and output to an array. I appreciate any help! Thanks.