0

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

geds133
  • 1,121
  • 3
  • 11
  • 32

3 Answers3

0

You can avoid loops using this approach:

#Data
df <- structure(list(preds = c(18L, 20L, 49L, 49L, 20L), group = c("(0,6.49e+03]", 
"(0,6.49e+04]", "(0,6.49e+02]", "(0,6.49e+03]", "(0,6.49e+04]"
)), class = "data.frame", row.names = c("1", "2", "3", "4", "5"
))

The code:

#Code
aggregate(preds~group,data=df,function(x) paste0(x,collapse = ', '))

The output:

         group  preds
1 (0,6.49e+02]     49
2 (0,6.49e+03] 18, 49
3 (0,6.49e+04] 20, 20
Duck
  • 38,442
  • 13
  • 38
  • 79
0

Maybe you can try tapply

with(df,tapply(preds,group,c))

or split

with(df,split(preds,group))

which gives

$`(0,6.49e+02]`
[1] 49

$`(0,6.49e+03]`
[1] 18 49

$`(0,6.49e+04]`
[1] 20 20
ThomasIsCoding
  • 80,151
  • 7
  • 17
  • 65
0

If you really want to use your for loop here is an adapted version that gets at what you want.

#Data
preds<-c(18,20,49,49,20)
group<-c("a","b","c","a","b")
df<-data.frame(preds,group)
for (i in 1:length(unique(levels(df$group)))){
  group<-(unique(levels(df$group))[i])
  Value<-(df$preds[df['group'] == unique(levels(df$group))[i]])
  print(paste(group, Value))
  }
Tanner33
  • 122
  • 2
  • 15