R: grouping the values of a dataframe in ranges

0

Considering a dataframe like this:

 a$ID<-data.frame(c("PV001","PV001","PV002","PV002","PV003","PV003"))
 b$freq<-data.frame(c("468","487","213896","212005","90827","86592"))

 c<-cbind(a,b);colnames(c)<-c("ID","freq")

I wanted to know how to group the data according to the ID value and make a range of the values of freq.

Thanks in advance, Greetings.

    
asked by Adrián P.L. 12.12.2018 в 19:49
source

1 answer

2

With R base you can solve it with aggregate() . I'm going to modify your example because as it would be c would not be a data.frame :

c <- data.frame(ID=c("PV001","PV001","PV002","PV002","PV003","PV003"),
                 Freq=c("468","487","213896","212005","90827","86592"),
                 stringsAsFactors = FALSE)

aggregate(Freq ~ ID, c, range)

     ID Freq.1 Freq.2
1 PV001    468    487
2 PV002 212005 213896
3 PV003  86592  90827

Notes:

  • We can use the formula notation Freq ~ ID that is the frequency based on the ID , the next parameter is the data.frame to group
  • Finally, we apply the function range() to each group of ID
answered by 12.12.2018 / 20:32
source