4

I am trying to group by year and sum the weight for each year but when my new data frame is created the column names begin with an annoying "X" like "X2000" instead of 2000. Of course I could just delete it or replace it with "" after creation but I want it not be created from the very beginning since this method will be applied to dataframes with more columns.

library(tidyverse)

year<-c("2000","2000","2001","2002","2000","2002")
weight<-c(0.5,0.7,0.8,0.7,0.6,0.9)
YG<-data.frame(year,weight)


w<-data.frame(YG %>%
                     group_by(year) %>%
                     summarise(n = round(sum(weight)),
                               g = n()) %>%
                     select(-g) %>%
                     spread(year, n, fill = 0))
M--
  • 25,431
  • 8
  • 61
  • 93
firmo23
  • 7,490
  • 2
  • 38
  • 114
  • 3
    The problem is you're attempting to replace colnames with numeric names. Perhaps set colnames to something non-numeric? – NelsonGon Jan 02 '19 at 18:04
  • 1
    Why do you need to wrap with `data.frame`. A `tibble` output is fine – akrun Jan 02 '19 at 18:07
  • 2
    For more info, check [here](https://stackoverflow.com/questions/10441437/why-am-i-getting-x-in-my-column-names-when-reading-a-data-frame) – akrun Jan 02 '19 at 18:10
  • I manipulate it easier later but I understand your question – firmo23 Jan 02 '19 at 18:10

2 Answers2

7

While not really advisable, what you want is check.names = FALSE in the data.frame call:

data.frame(YG %>% group_by(year) %>%
             summarise(n = round(sum(weight)), g = n()) %>%
             select(-g) %>% spread(year, n, fill = 0), 
           check.names = FALSE)
#   2000 2001 2002
# 1    2    1    2
Julius Vainora
  • 47,421
  • 9
  • 90
  • 102
3

An alternative: new_YG is w

names(new_YG)<-sapply(str_remove_all(colnames(new_YG),"X"),"[")
names(new_YG)
new_YG
NelsonGon
  • 13,015
  • 7
  • 27
  • 57