Remove the “X” letter from the column names of a new dataframe [duplicate]
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))
r
marked as duplicate by divibisan, Henrik
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.
add a comment |
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))
r
marked as duplicate by divibisan, Henrik
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 withdata.frame
. Atibble
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
add a comment |
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))
r
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
r
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
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
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 withdata.frame
. Atibble
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
add a comment |
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 withdata.frame
. Atibble
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
add a comment |
2 Answers
2
active
oldest
votes
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
2
Good to know that option is there. Usingas.data.frame
(no options) also works
– IceCreamToucan
Jan 2 at 18:21
add a comment |
An alternative: new_YG is w
names(new_YG)<-sapply(str_remove_all(colnames(new_YG),"X"),"[")
names(new_YG)
new_YG
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
2
Good to know that option is there. Usingas.data.frame
(no options) also works
– IceCreamToucan
Jan 2 at 18:21
add a comment |
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
2
Good to know that option is there. Usingas.data.frame
(no options) also works
– IceCreamToucan
Jan 2 at 18:21
add a comment |
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
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
answered Jan 2 at 18:03
Julius VainoraJulius Vainora
38.3k76786
38.3k76786
2
Good to know that option is there. Usingas.data.frame
(no options) also works
– IceCreamToucan
Jan 2 at 18:21
add a comment |
2
Good to know that option is there. Usingas.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
add a comment |
An alternative: new_YG is w
names(new_YG)<-sapply(str_remove_all(colnames(new_YG),"X"),"[")
names(new_YG)
new_YG
add a comment |
An alternative: new_YG is w
names(new_YG)<-sapply(str_remove_all(colnames(new_YG),"X"),"[")
names(new_YG)
new_YG
add a comment |
An alternative: new_YG is w
names(new_YG)<-sapply(str_remove_all(colnames(new_YG),"X"),"[")
names(new_YG)
new_YG
An alternative: new_YG is w
names(new_YG)<-sapply(str_remove_all(colnames(new_YG),"X"),"[")
names(new_YG)
new_YG
answered Jan 2 at 18:07
NelsonGonNelsonGon
3,4373833
3,4373833
add a comment |
add a comment |
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
. Atibble
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