0

I have a DF called data that has multiple columns. One of them is a column called 'swim_hours' that is a column of integers. I want to use dyplr to do some manipulations on this DF, but I also want to keep this column intact, though I am not manipulating it in any way. My code is as follows: trial_swim = data %>% group_by(penguin, date, trial, location) %>% summarise(swim_t = as.numeric(datetime[n()] - datetime[1])) %>% summarise(hours = swim_hours)

Again, the purpose of the last line is to simply maintain my 'swim_hours' column into my new DF 'trial_swim'.

When I run this, I get the error message: Error in summarise_impl(.data, dots) : Evaluation error: object 'swim_hour' not found.

Clearly 'swim_hours' is in my 'data' DF, why can't it find it?

Is there an easier way to keep a column that is not being manipulated when using dyplr and pipes??

aosmith
  • 34,856
  • 9
  • 84
  • 118
Katie
  • 323
  • 3
  • 10
  • 1
    You are looking for `mutate` not `summarise` – BENY Jul 05 '17 at 20:39
  • If you are using `group_by` I don't think is possible to keep the column intact. You can create a new version of the column aggregated in a certain way. Can you provide a reproducible example? – amarchin Jul 05 '17 at 20:58
  • It would be easier to help if you provided a [reproducibleexample](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input data and the desired output data. Trying to use both a `summarize()` and yet still preserve row-level data seems contradictory. – MrFlick Jul 05 '17 at 21:04
  • If this variable has one unique integer per grouping, you could include this variable as a grouping variable. Or make a second variable in your `summarise` call with, e.g., `unique(swim_hours)`. – aosmith Jul 05 '17 at 21:21
  • I would simply try to create a copy of the column swim_hours using mutate at the top of the pipe... mutate(swim_hrs= swim_hours)%>%. Can you provide a dput of the df? – petergensler Jul 06 '17 at 01:42

0 Answers0