1

I have a csv as seen in image 1 and I want to group the data by dates and its count as seen in second image:

enter image description here

When I try the below code I get dates as X1.22.20 format. How can I overcome this?

bar <- subset(data.raw, Country.Region == "China")
head(bar)
final_df <- as.data.frame(t(bar))

umn

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
roshualine
  • 81
  • 1
  • 11
  • 3
    If you want to sum all the count in each date i.e columns, you could use `colSums`. `colSums(data.raw[-c(1:2)])` and please add data using `dput` and not as images. – Ronak Shah Mar 29 '20 at 10:24
  • 1
    Please provide [some reproducible code](https://stackoverflow.com/help/minimal-reproducible-example) to increase your chances of getting help. For your example we cannot reproduce your issue. – DJJ Mar 29 '20 at 10:37
  • 1
    If you're reading a csv file that has dates as header names, you can add `check.names = FALSE` in `read.csv`. Just be aware that those column names could become problematic. To put your data into long form, consider `pivot_longer` from `tidyr` or [other approach](https://stackoverflow.com/questions/2185252/reshaping-data-frame-from-wide-to-long-format) and then `group_by` date. As suggested above, more detailed assistance can be provided you post data with `dput()` and provide reproducible code with desired result. – Ben Mar 29 '20 at 13:56

1 Answers1

0

I suppose you want to do something like that: You'd rather read your file with the readr package and then use the pivot_longer() function to set your date with the lubridate::dmy() function.

library(readr)
library(lubridate)
db = read_csv("...")
db = db%>%
group_by("...") %>% #I guess "country"
  #Delete what you don't need 
  select(-"...") %>% 
    pivot_longer(ends_with("20"), 
                 names_to = "date", 
                 values_to = "value") %>%
  mutate(date = lubridate::dmy(date))

cdcarrion
  • 574
  • 6
  • 22