0

Using dplyr in R (microsoft R Open 3.5.3 to be precise). I'm having a slight problem with dplyr whereby I'm sometimes seeing lots of additional information in the data frame I create. For example, for these lines of code:

claims_frame_2 <- left_join(claims_frame,
                            select(new_policy_frame, c(Lookup_Key_4, Exposure_Year, RowName)),
                            by = c("Accident_Year" = "Exposure_Year", "Lookup_Key_4" = "Lookup_Key_4")
)

claims_frame_3 <- claims_frame_2 %>% group_by(Claim.Number) %>% filter(RowName == max(RowName))

No problem with the left_join command, but when I do the second command (group by/filter), the data structure of the claims_frame_3 object is different to that of the claims_frame_2 object. Seems to suddenly have lots of attributes (something I know little about) attached to the RowName field. See the attached photo.

enter image description here

Does anyone know why this happens and how I can stop it?

I had hoped to put together a small chunk of reproducible code that demonstrated this happening, but so far I haven't been successful. I will continue. In the mean time, I'm hoping someone might see this code (from a real project) and immediately know why this is happening!

Grateful for any advice. Thanks Alan

Alan
  • 619
  • 6
  • 19
  • 4
    It shows a `list` column. Please check the `str` of your input datasets. if the group attributes are the issue, then `ungroup` afterwards – akrun Nov 04 '19 at 19:48
  • 3
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Nov 04 '19 at 19:50
  • 1
    To add to @akrun's comment, you have a `group_by(Claim.Number)` step, and this is adding some grouping information to the data frame in the object explorer. Adding `%>% ungroup()` at the end of that dplyr chain will remove the grouping and make it look less cluttered in the object explorer. – Jon Spring Nov 04 '19 at 20:44
  • 1
    Are you sure you are using `dplyr::filter` and not another `filter` function? Try writing `filter` alone in the console. – Dan Chaltiel Nov 05 '19 at 15:56
  • Thank you all. I found that putting %>% ungroup() at the end of the dplyr chains fixed the problems. I can't pretend that I fully understand why...but I'm just happy that it works! – Alan Nov 05 '19 at 18:59
  • Apologies for lack of a reproducible example. I always try to include one of these when I post questions on here, but alas I couldn't get it to work :-( – Alan Nov 05 '19 at 19:01

0 Answers0