Skip to content
Prev 299023 / 398506 Next

Data manipulation with aggregate

Hi everyone.

I have these data :

myData = data.frame(Name = c('a', 'a', 'b', 'b'), length = c(1,2,3,4), type
= c('x','x','y','z'))

which gives me:

  Name length type
1    a      1    x
2    a      2    x
3    b      3    y
4    b      4   z

I would group (mean) this DF using 'Name' as grouping factor. However, I
have a field ('type') which is a string. I would like to use the unique
value of this field when possible (i.e. when all the 'type' values are the
same for each group) or replace with NA when 'type' has multiple values.

In fact, I would like to obtain this:

  Name length type
1    a      1.5    x
2    b      3.5    NA

For instance, I was using this command:

aggregate(list(myData$length, myData$type), list(myData$Name), FUN = mean)

But it can't deal with string data.

I hope I have been clear enough.

With regards,
Phil

--
View this message in context: http://r.789695.n4.nabble.com/Data-manipulation-with-aggregate-tp4635298.html
Sent from the R help mailing list archive at Nabble.com.