Remove the “X” letter from the column names of a new dataframe [duplicate]












3
















This question already has an answer here:




  • Numbers as column names of data frames

    2 answers



  • Why am I getting X. in my column names when reading a data frame?

    4 answers



  • Specifying column names in a data.frame changes spaces to “.”

    4 answers




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))









share|improve this question















marked as duplicate by divibisan, Henrik r
Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 18:31


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 3





    The problem is you're attempting to replace colnames with numeric names. Perhaps set colnames to something non-numeric?

    – NelsonGon
    Jan 2 at 18:04






  • 1





    Why do you need to wrap with data.frame. A tibble output is fine

    – akrun
    Jan 2 at 18:07






  • 2





    For more info, check here

    – akrun
    Jan 2 at 18:10











  • I manipulate it easier later but I understand your question

    – firmo23
    Jan 2 at 18:10
















3
















This question already has an answer here:




  • Numbers as column names of data frames

    2 answers



  • Why am I getting X. in my column names when reading a data frame?

    4 answers



  • Specifying column names in a data.frame changes spaces to “.”

    4 answers




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))









share|improve this question















marked as duplicate by divibisan, Henrik r
Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 18:31


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 3





    The problem is you're attempting to replace colnames with numeric names. Perhaps set colnames to something non-numeric?

    – NelsonGon
    Jan 2 at 18:04






  • 1





    Why do you need to wrap with data.frame. A tibble output is fine

    – akrun
    Jan 2 at 18:07






  • 2





    For more info, check here

    – akrun
    Jan 2 at 18:10











  • I manipulate it easier later but I understand your question

    – firmo23
    Jan 2 at 18:10














3












3








3









This question already has an answer here:




  • Numbers as column names of data frames

    2 answers



  • Why am I getting X. in my column names when reading a data frame?

    4 answers



  • Specifying column names in a data.frame changes spaces to “.”

    4 answers




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))









share|improve this question

















This question already has an answer here:




  • Numbers as column names of data frames

    2 answers



  • Why am I getting X. in my column names when reading a data frame?

    4 answers



  • Specifying column names in a data.frame changes spaces to “.”

    4 answers




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))




This question already has an answer here:




  • Numbers as column names of data frames

    2 answers



  • Why am I getting X. in my column names when reading a data frame?

    4 answers



  • Specifying column names in a data.frame changes spaces to “.”

    4 answers








r






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 18:04









M-M

7,14962146




7,14962146










asked Jan 2 at 18:00









firmo23firmo23

870516




870516




marked as duplicate by divibisan, Henrik r
Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 18:31


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by divibisan, Henrik r
Users with the  r badge can single-handedly close r questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 2 at 18:31


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 3





    The problem is you're attempting to replace colnames with numeric names. Perhaps set colnames to something non-numeric?

    – NelsonGon
    Jan 2 at 18:04






  • 1





    Why do you need to wrap with data.frame. A tibble output is fine

    – akrun
    Jan 2 at 18:07






  • 2





    For more info, check here

    – akrun
    Jan 2 at 18:10











  • I manipulate it easier later but I understand your question

    – firmo23
    Jan 2 at 18:10














  • 3





    The problem is you're attempting to replace colnames with numeric names. Perhaps set colnames to something non-numeric?

    – NelsonGon
    Jan 2 at 18:04






  • 1





    Why do you need to wrap with data.frame. A tibble output is fine

    – akrun
    Jan 2 at 18:07






  • 2





    For more info, check here

    – akrun
    Jan 2 at 18:10











  • I manipulate it easier later but I understand your question

    – firmo23
    Jan 2 at 18:10








3




3





The problem is you're attempting to replace colnames with numeric names. Perhaps set colnames to something non-numeric?

– NelsonGon
Jan 2 at 18:04





The problem is you're attempting to replace colnames with numeric names. Perhaps set colnames to something non-numeric?

– NelsonGon
Jan 2 at 18:04




1




1





Why do you need to wrap with data.frame. A tibble output is fine

– akrun
Jan 2 at 18:07





Why do you need to wrap with data.frame. A tibble output is fine

– akrun
Jan 2 at 18:07




2




2





For more info, check here

– akrun
Jan 2 at 18:10





For more info, check here

– akrun
Jan 2 at 18:10













I manipulate it easier later but I understand your question

– firmo23
Jan 2 at 18:10





I manipulate it easier later but I understand your question

– firmo23
Jan 2 at 18:10












2 Answers
2






active

oldest

votes


















3














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





share|improve this answer



















  • 2





    Good to know that option is there. Using as.data.frame (no options) also works

    – IceCreamToucan
    Jan 2 at 18:21





















1














An alternative: new_YG is w



names(new_YG)<-sapply(str_remove_all(colnames(new_YG),"X"),"[")
names(new_YG)
new_YG





share|improve this answer






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    3














    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





    share|improve this answer



















    • 2





      Good to know that option is there. Using as.data.frame (no options) also works

      – IceCreamToucan
      Jan 2 at 18:21


















    3














    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





    share|improve this answer



















    • 2





      Good to know that option is there. Using as.data.frame (no options) also works

      – IceCreamToucan
      Jan 2 at 18:21
















    3












    3








    3







    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





    share|improve this answer













    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






    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 2 at 18:03









    Julius VainoraJulius Vainora

    38.3k76786




    38.3k76786








    • 2





      Good to know that option is there. Using as.data.frame (no options) also works

      – IceCreamToucan
      Jan 2 at 18:21
















    • 2





      Good to know that option is there. Using as.data.frame (no options) also works

      – IceCreamToucan
      Jan 2 at 18:21










    2




    2





    Good to know that option is there. Using as.data.frame (no options) also works

    – IceCreamToucan
    Jan 2 at 18:21







    Good to know that option is there. Using as.data.frame (no options) also works

    – IceCreamToucan
    Jan 2 at 18:21















    1














    An alternative: new_YG is w



    names(new_YG)<-sapply(str_remove_all(colnames(new_YG),"X"),"[")
    names(new_YG)
    new_YG





    share|improve this answer




























      1














      An alternative: new_YG is w



      names(new_YG)<-sapply(str_remove_all(colnames(new_YG),"X"),"[")
      names(new_YG)
      new_YG





      share|improve this answer


























        1












        1








        1







        An alternative: new_YG is w



        names(new_YG)<-sapply(str_remove_all(colnames(new_YG),"X"),"[")
        names(new_YG)
        new_YG





        share|improve this answer













        An alternative: new_YG is w



        names(new_YG)<-sapply(str_remove_all(colnames(new_YG),"X"),"[")
        names(new_YG)
        new_YG






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 2 at 18:07









        NelsonGonNelsonGon

        3,4373833




        3,4373833















            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith