I am fairly new to R and am trying get all dataframe rows from one column that correspond to unique levels in another. My dataframe, called df
, has 2 columns: preds
and group
which contains 20 unique levels. I am trying to get all the values of preds
for each individual level in group
.
An example of the dataframe is as such:
preds group
1 18 (0,6.49e+03]
2 20 (0,6.49e+04]
3 49 (0,6.49e+02]
4 49 (0,6.49e+03]
5 20 (0,6.49e+04]
My for loop to try and get this is as follow:
for (i in unique(levels(df$group))){
results <- df$preds[df['group'] == i]
print(i)
print(results)}
This should print the preds
for unique levels and look as such:
(0,6.49e+03]
18, 49
(0,6.49e+04]
20, 20
(0,6.49e+02]
49
However this seems to just print an empty vector everytime. Can someone help me to understand how to do this and if I am even attempting this the correct way at all?
Thanks