Reputation: 305
I have a data.frame
with a set of records and as variables different measurements. I would like to create a new data.frame
containing the amount of records having a specific measurement value for each measurement. Basically what I am trying to do is:
record <- c("r1", "r2", "r3")
firstMeasurement <- c(15, 10, 10)
secondMeasurement <- c(2, 4, 2)
df <- data.frame(record, firstMeasurement, secondMeasurement)
measurements <- c(colnames(df[2:3]))
measuramentsAggregate <- lapply(measurements, function(i)
aggregate(record~i, df, FUN=length))
I am getting really funny errors and I don't understand why. Can anyone help me?
Many thanks!
Upvotes: 0
Views: 203
Reputation: 382
I think this is what you want
library(dplyr)
agg.measurements <- df %>% group_by(firstMeasurement) %>% summarise(records=n())
That should do it for the one.
Upvotes: 1
Reputation: 738
If you want the number of records with specific firstMeasurement
:
table(df$firstMeasurement)
Likewise for the secondMeasurement
. I am not sure how the data.frame
you are trying to create might look.
Upvotes: 1