Home dplyr summarize by string
Reply: 0

dplyr summarize by string

user1170 Published in June 20, 2018, 7:44 am

I have a dataframe that has numeric and string values, for example:

 mydf <- data.frame(id = c(1, 2, 1, 2, 3, 4),
               value = c(32, 12, 43, 6, 50, 20),
               text = c('A', 'B', 'A', 'B', 'C', 'D'))

The value of id variable always corresponds to text variable, e.g., id == 1 will always be text == 'A'.

Now, I want to summarize this dataframe by id (or by text, since it's the same thing):

mydf %>%
  group_by(id) %>%
  summarize(mean_value = mean(value))

This works nicely, but I also need the text variable, since I wan t to do text analysis.

However, when I add text to the dplyr pipe:

mydf %>%
  group_by(id) %>%
  summarize(mean_value = mean(value),
  text = text)

I get the following error:

Error: expecting a single value

Since text for id is always the same, is it possible to append it to the summarized dataframe?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.327892 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO