Help with R Code

Just learning the R code, and I am trying to make what I thought would be simple script which has not been easy, I am importing a excel file with no issue there, but want to make a script that will remove duplicate name but take the there hours and claims and add to the single name.. example would be
Name Hours Claims
Jack 1 1
Jack 1 1
Paul 1 1

would like it to do this.
Name Hours Claims
Jack 2 2
Paul 1 1

If the data frame is named DF, I would use

DFsummary <- DF %>% group_by(Name) %>%
   summarize(Hours = sum(Hours), Claims = sum(Claims))

Does that work for you?

1 Like

I will give that a try, I was trying rowsums and colsums before.. and wasn't getting very far.

I forgot to include that the functions I used are from the dplyr package. Add

library(dplyr)

to the beginning of the code.

yeah I figured I would need dplyr but you never know for sure sometimes, will run it and report back when I get back to house..

is there a step I am missing I am getting Error object not found Hours and claims.. I imported the data in Rstudio and the excel file is loaded and showing the data in top left corner of screen.. or do I need other commands so it uses the data that was imported.

It seems the data frame you are using in the code does not have columns named Hours or Claims. Please post the result of running the code

str(DF)

except replacing DF with whatever name you have given to the data you imported.

tibble [3 x 3] (S3: tbl_df/tbl/data.frame)
Name : chr [1:3] "Jack" "Jack" "Paul" Hours : num [1:3] 1 1 1
$ Claims: num [1:3] 1 1 1

I figured out the partial mistake I was using the DF instead of the actual name of the file I was importing.. now just have to tinker alittle more, I got it to sort of work except it added 2 and 2 to both names instead of just the one. oh yeah thanks for the help got me further then before.

Here is a full example with a small data frame.

DF <- data.frame(Name = c("Jack", "Jack", "Paul"),
                 Hours= c(1,1,1),
                 Claims = c(1,1,1))
library(dplyr, warn.conflicts = FALSE)
DFsummary <- DF %>% group_by(Name) %>% 
  summarize(Hours = sum(Hours), Claims = sum(Claims))
DFsummary
#> # A tibble: 2 x 3
#>   Name  Hours Claims
#>   <fct> <dbl>  <dbl>
#> 1 Jack      2      2
#> 2 Paul      1      1

Created on 2020-08-01 by the reprex package (v0.3.0)

aha ok I see what I was doing wrong.. thanks again your help is greatly appreciated.