Error in match.names(clabs, names(xi)) : names do not match previous names
- I am creating a vector having 5 age values and named it as boys_age. Likewise created a vector named
girls_age
.
eg :
boys_age <- c(18,15,16,17,19)
girls_age<- c(16,14,18,17,15)
Then Append
rbind()
the two vectors to create data.frame such that I have two columns namedgroup
andage
.The values from boys_age and girls_age should be in the column
age
. Thegroup
column should have the category values, boys/girls, to identify the source vector.
r rbind
add a comment |
- I am creating a vector having 5 age values and named it as boys_age. Likewise created a vector named
girls_age
.
eg :
boys_age <- c(18,15,16,17,19)
girls_age<- c(16,14,18,17,15)
Then Append
rbind()
the two vectors to create data.frame such that I have two columns namedgroup
andage
.The values from boys_age and girls_age should be in the column
age
. Thegroup
column should have the category values, boys/girls, to identify the source vector.
r rbind
add a comment |
- I am creating a vector having 5 age values and named it as boys_age. Likewise created a vector named
girls_age
.
eg :
boys_age <- c(18,15,16,17,19)
girls_age<- c(16,14,18,17,15)
Then Append
rbind()
the two vectors to create data.frame such that I have two columns namedgroup
andage
.The values from boys_age and girls_age should be in the column
age
. Thegroup
column should have the category values, boys/girls, to identify the source vector.
r rbind
- I am creating a vector having 5 age values and named it as boys_age. Likewise created a vector named
girls_age
.
eg :
boys_age <- c(18,15,16,17,19)
girls_age<- c(16,14,18,17,15)
Then Append
rbind()
the two vectors to create data.frame such that I have two columns namedgroup
andage
.The values from boys_age and girls_age should be in the column
age
. Thegroup
column should have the category values, boys/girls, to identify the source vector.
r rbind
r rbind
edited Nov 20 '18 at 9:28
Sotos
30k51640
30k51640
asked Nov 20 '18 at 9:26
Himanshu GehlotHimanshu Gehlot
13
13
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Its actually the most primal thing to do in R:
data:
df1 <- data.frame(boys_age = c(18,15,16,17,19), girls_age = c(16,14,18,17,15))
code:
library(data.table)
melt(setDT(df1), variable.name = "group", value.name = "age", measure.vars = c("boys_age", "girls_age"))[,2:1][,group:=sub("_.*$","",group)]
result:
# age group
# 1: 18 boys
# 2: 15 boys
# 3: 16 boys
# 4: 17 boys
# 5: 19 boys
# 6: 16 girls
# 7: 14 girls
# 8: 18 girls
# 9: 17 girls
#10: 15 girls
You seem to be keen on using ?rbind
: (not practical though)
rbind(
cbind.data.frame(age = df1$boys_age, group = "boys"),
cbind.data.frame(age = df1$girls_age, group = "girls")
)
# age group
#1 18 boys
#2 15 boys
#3 16 boys
#4 17 boys
#5 19 boys
#6 16 girls
#7 14 girls
#8 18 girls
#9 17 girls
#10 15 girls
- In the
?cbind
section I'm making use of the recycling functionality R provides. Read about it. - Why am I using
cbind.data.frame
, otherwise cbind would create a matrix and therefore the age numerics would be converted to characters.
add a comment |
Here is one option using stack
out <- stack(list(boys = boys_age, girls = girls_age))
out
# values ind
#1 18 boys
#2 15 boys
#3 16 boys
#4 17 boys
#5 19 boys
#6 16 girls
#7 14 girls
#8 18 girls
#9 17 girls
#10 15 girls
Now change names
names(out) <- c("age", "group")
out
# age group
#1 18 boys
#2 15 boys
#3 16 boys
#4 17 boys
#5 19 boys
#6 16 girls
#7 14 girls
#8 18 girls
#9 17 girls
#10 15 girls
You could also do the same in one line, thanks to @Sotos
setNames(stack(list(boys = boys_age, girls = girls_age)), c('age', 'group'))
3
Nice 1. You can also do all in 1 line, i.e.setNames(stack(list(boys = boys_age, girls = girls_age)), c('age', 'group'))
– Sotos
Nov 20 '18 at 9:37
Thank you, Markus, for your response. But the problem needs to be solved by using data.frame() and rbind() only. It's an exercise for data.frame and rbind function.
– Himanshu Gehlot
Nov 20 '18 at 9:39
1
That smells like homework...
– Sotos
Nov 20 '18 at 9:41
Not exactly, i am making some practice questions and got stuck with this.
– Himanshu Gehlot
Nov 20 '18 at 9:43
2
@HimanshuGehlot You can't do it directly withrbind()
anddata.frame()
only. You could do something likesetNames(stack(data.frame(t(rbind(boys_age, girls_age)))), c('age', 'group'))
which includes them, but are not explicit
– Sotos
Nov 20 '18 at 10:00
|
show 7 more comments
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53389851%2ferror-in-match-namesclabs-namesxi-names-do-not-match-previous-names%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Its actually the most primal thing to do in R:
data:
df1 <- data.frame(boys_age = c(18,15,16,17,19), girls_age = c(16,14,18,17,15))
code:
library(data.table)
melt(setDT(df1), variable.name = "group", value.name = "age", measure.vars = c("boys_age", "girls_age"))[,2:1][,group:=sub("_.*$","",group)]
result:
# age group
# 1: 18 boys
# 2: 15 boys
# 3: 16 boys
# 4: 17 boys
# 5: 19 boys
# 6: 16 girls
# 7: 14 girls
# 8: 18 girls
# 9: 17 girls
#10: 15 girls
You seem to be keen on using ?rbind
: (not practical though)
rbind(
cbind.data.frame(age = df1$boys_age, group = "boys"),
cbind.data.frame(age = df1$girls_age, group = "girls")
)
# age group
#1 18 boys
#2 15 boys
#3 16 boys
#4 17 boys
#5 19 boys
#6 16 girls
#7 14 girls
#8 18 girls
#9 17 girls
#10 15 girls
- In the
?cbind
section I'm making use of the recycling functionality R provides. Read about it. - Why am I using
cbind.data.frame
, otherwise cbind would create a matrix and therefore the age numerics would be converted to characters.
add a comment |
Its actually the most primal thing to do in R:
data:
df1 <- data.frame(boys_age = c(18,15,16,17,19), girls_age = c(16,14,18,17,15))
code:
library(data.table)
melt(setDT(df1), variable.name = "group", value.name = "age", measure.vars = c("boys_age", "girls_age"))[,2:1][,group:=sub("_.*$","",group)]
result:
# age group
# 1: 18 boys
# 2: 15 boys
# 3: 16 boys
# 4: 17 boys
# 5: 19 boys
# 6: 16 girls
# 7: 14 girls
# 8: 18 girls
# 9: 17 girls
#10: 15 girls
You seem to be keen on using ?rbind
: (not practical though)
rbind(
cbind.data.frame(age = df1$boys_age, group = "boys"),
cbind.data.frame(age = df1$girls_age, group = "girls")
)
# age group
#1 18 boys
#2 15 boys
#3 16 boys
#4 17 boys
#5 19 boys
#6 16 girls
#7 14 girls
#8 18 girls
#9 17 girls
#10 15 girls
- In the
?cbind
section I'm making use of the recycling functionality R provides. Read about it. - Why am I using
cbind.data.frame
, otherwise cbind would create a matrix and therefore the age numerics would be converted to characters.
add a comment |
Its actually the most primal thing to do in R:
data:
df1 <- data.frame(boys_age = c(18,15,16,17,19), girls_age = c(16,14,18,17,15))
code:
library(data.table)
melt(setDT(df1), variable.name = "group", value.name = "age", measure.vars = c("boys_age", "girls_age"))[,2:1][,group:=sub("_.*$","",group)]
result:
# age group
# 1: 18 boys
# 2: 15 boys
# 3: 16 boys
# 4: 17 boys
# 5: 19 boys
# 6: 16 girls
# 7: 14 girls
# 8: 18 girls
# 9: 17 girls
#10: 15 girls
You seem to be keen on using ?rbind
: (not practical though)
rbind(
cbind.data.frame(age = df1$boys_age, group = "boys"),
cbind.data.frame(age = df1$girls_age, group = "girls")
)
# age group
#1 18 boys
#2 15 boys
#3 16 boys
#4 17 boys
#5 19 boys
#6 16 girls
#7 14 girls
#8 18 girls
#9 17 girls
#10 15 girls
- In the
?cbind
section I'm making use of the recycling functionality R provides. Read about it. - Why am I using
cbind.data.frame
, otherwise cbind would create a matrix and therefore the age numerics would be converted to characters.
Its actually the most primal thing to do in R:
data:
df1 <- data.frame(boys_age = c(18,15,16,17,19), girls_age = c(16,14,18,17,15))
code:
library(data.table)
melt(setDT(df1), variable.name = "group", value.name = "age", measure.vars = c("boys_age", "girls_age"))[,2:1][,group:=sub("_.*$","",group)]
result:
# age group
# 1: 18 boys
# 2: 15 boys
# 3: 16 boys
# 4: 17 boys
# 5: 19 boys
# 6: 16 girls
# 7: 14 girls
# 8: 18 girls
# 9: 17 girls
#10: 15 girls
You seem to be keen on using ?rbind
: (not practical though)
rbind(
cbind.data.frame(age = df1$boys_age, group = "boys"),
cbind.data.frame(age = df1$girls_age, group = "girls")
)
# age group
#1 18 boys
#2 15 boys
#3 16 boys
#4 17 boys
#5 19 boys
#6 16 girls
#7 14 girls
#8 18 girls
#9 17 girls
#10 15 girls
- In the
?cbind
section I'm making use of the recycling functionality R provides. Read about it. - Why am I using
cbind.data.frame
, otherwise cbind would create a matrix and therefore the age numerics would be converted to characters.
edited Nov 20 '18 at 11:29
answered Nov 20 '18 at 9:48
Andre ElricoAndre Elrico
5,72311229
5,72311229
add a comment |
add a comment |
Here is one option using stack
out <- stack(list(boys = boys_age, girls = girls_age))
out
# values ind
#1 18 boys
#2 15 boys
#3 16 boys
#4 17 boys
#5 19 boys
#6 16 girls
#7 14 girls
#8 18 girls
#9 17 girls
#10 15 girls
Now change names
names(out) <- c("age", "group")
out
# age group
#1 18 boys
#2 15 boys
#3 16 boys
#4 17 boys
#5 19 boys
#6 16 girls
#7 14 girls
#8 18 girls
#9 17 girls
#10 15 girls
You could also do the same in one line, thanks to @Sotos
setNames(stack(list(boys = boys_age, girls = girls_age)), c('age', 'group'))
3
Nice 1. You can also do all in 1 line, i.e.setNames(stack(list(boys = boys_age, girls = girls_age)), c('age', 'group'))
– Sotos
Nov 20 '18 at 9:37
Thank you, Markus, for your response. But the problem needs to be solved by using data.frame() and rbind() only. It's an exercise for data.frame and rbind function.
– Himanshu Gehlot
Nov 20 '18 at 9:39
1
That smells like homework...
– Sotos
Nov 20 '18 at 9:41
Not exactly, i am making some practice questions and got stuck with this.
– Himanshu Gehlot
Nov 20 '18 at 9:43
2
@HimanshuGehlot You can't do it directly withrbind()
anddata.frame()
only. You could do something likesetNames(stack(data.frame(t(rbind(boys_age, girls_age)))), c('age', 'group'))
which includes them, but are not explicit
– Sotos
Nov 20 '18 at 10:00
|
show 7 more comments
Here is one option using stack
out <- stack(list(boys = boys_age, girls = girls_age))
out
# values ind
#1 18 boys
#2 15 boys
#3 16 boys
#4 17 boys
#5 19 boys
#6 16 girls
#7 14 girls
#8 18 girls
#9 17 girls
#10 15 girls
Now change names
names(out) <- c("age", "group")
out
# age group
#1 18 boys
#2 15 boys
#3 16 boys
#4 17 boys
#5 19 boys
#6 16 girls
#7 14 girls
#8 18 girls
#9 17 girls
#10 15 girls
You could also do the same in one line, thanks to @Sotos
setNames(stack(list(boys = boys_age, girls = girls_age)), c('age', 'group'))
3
Nice 1. You can also do all in 1 line, i.e.setNames(stack(list(boys = boys_age, girls = girls_age)), c('age', 'group'))
– Sotos
Nov 20 '18 at 9:37
Thank you, Markus, for your response. But the problem needs to be solved by using data.frame() and rbind() only. It's an exercise for data.frame and rbind function.
– Himanshu Gehlot
Nov 20 '18 at 9:39
1
That smells like homework...
– Sotos
Nov 20 '18 at 9:41
Not exactly, i am making some practice questions and got stuck with this.
– Himanshu Gehlot
Nov 20 '18 at 9:43
2
@HimanshuGehlot You can't do it directly withrbind()
anddata.frame()
only. You could do something likesetNames(stack(data.frame(t(rbind(boys_age, girls_age)))), c('age', 'group'))
which includes them, but are not explicit
– Sotos
Nov 20 '18 at 10:00
|
show 7 more comments
Here is one option using stack
out <- stack(list(boys = boys_age, girls = girls_age))
out
# values ind
#1 18 boys
#2 15 boys
#3 16 boys
#4 17 boys
#5 19 boys
#6 16 girls
#7 14 girls
#8 18 girls
#9 17 girls
#10 15 girls
Now change names
names(out) <- c("age", "group")
out
# age group
#1 18 boys
#2 15 boys
#3 16 boys
#4 17 boys
#5 19 boys
#6 16 girls
#7 14 girls
#8 18 girls
#9 17 girls
#10 15 girls
You could also do the same in one line, thanks to @Sotos
setNames(stack(list(boys = boys_age, girls = girls_age)), c('age', 'group'))
Here is one option using stack
out <- stack(list(boys = boys_age, girls = girls_age))
out
# values ind
#1 18 boys
#2 15 boys
#3 16 boys
#4 17 boys
#5 19 boys
#6 16 girls
#7 14 girls
#8 18 girls
#9 17 girls
#10 15 girls
Now change names
names(out) <- c("age", "group")
out
# age group
#1 18 boys
#2 15 boys
#3 16 boys
#4 17 boys
#5 19 boys
#6 16 girls
#7 14 girls
#8 18 girls
#9 17 girls
#10 15 girls
You could also do the same in one line, thanks to @Sotos
setNames(stack(list(boys = boys_age, girls = girls_age)), c('age', 'group'))
edited Nov 20 '18 at 9:39
answered Nov 20 '18 at 9:33
markusmarkus
13.1k1234
13.1k1234
3
Nice 1. You can also do all in 1 line, i.e.setNames(stack(list(boys = boys_age, girls = girls_age)), c('age', 'group'))
– Sotos
Nov 20 '18 at 9:37
Thank you, Markus, for your response. But the problem needs to be solved by using data.frame() and rbind() only. It's an exercise for data.frame and rbind function.
– Himanshu Gehlot
Nov 20 '18 at 9:39
1
That smells like homework...
– Sotos
Nov 20 '18 at 9:41
Not exactly, i am making some practice questions and got stuck with this.
– Himanshu Gehlot
Nov 20 '18 at 9:43
2
@HimanshuGehlot You can't do it directly withrbind()
anddata.frame()
only. You could do something likesetNames(stack(data.frame(t(rbind(boys_age, girls_age)))), c('age', 'group'))
which includes them, but are not explicit
– Sotos
Nov 20 '18 at 10:00
|
show 7 more comments
3
Nice 1. You can also do all in 1 line, i.e.setNames(stack(list(boys = boys_age, girls = girls_age)), c('age', 'group'))
– Sotos
Nov 20 '18 at 9:37
Thank you, Markus, for your response. But the problem needs to be solved by using data.frame() and rbind() only. It's an exercise for data.frame and rbind function.
– Himanshu Gehlot
Nov 20 '18 at 9:39
1
That smells like homework...
– Sotos
Nov 20 '18 at 9:41
Not exactly, i am making some practice questions and got stuck with this.
– Himanshu Gehlot
Nov 20 '18 at 9:43
2
@HimanshuGehlot You can't do it directly withrbind()
anddata.frame()
only. You could do something likesetNames(stack(data.frame(t(rbind(boys_age, girls_age)))), c('age', 'group'))
which includes them, but are not explicit
– Sotos
Nov 20 '18 at 10:00
3
3
Nice 1. You can also do all in 1 line, i.e.
setNames(stack(list(boys = boys_age, girls = girls_age)), c('age', 'group'))
– Sotos
Nov 20 '18 at 9:37
Nice 1. You can also do all in 1 line, i.e.
setNames(stack(list(boys = boys_age, girls = girls_age)), c('age', 'group'))
– Sotos
Nov 20 '18 at 9:37
Thank you, Markus, for your response. But the problem needs to be solved by using data.frame() and rbind() only. It's an exercise for data.frame and rbind function.
– Himanshu Gehlot
Nov 20 '18 at 9:39
Thank you, Markus, for your response. But the problem needs to be solved by using data.frame() and rbind() only. It's an exercise for data.frame and rbind function.
– Himanshu Gehlot
Nov 20 '18 at 9:39
1
1
That smells like homework...
– Sotos
Nov 20 '18 at 9:41
That smells like homework...
– Sotos
Nov 20 '18 at 9:41
Not exactly, i am making some practice questions and got stuck with this.
– Himanshu Gehlot
Nov 20 '18 at 9:43
Not exactly, i am making some practice questions and got stuck with this.
– Himanshu Gehlot
Nov 20 '18 at 9:43
2
2
@HimanshuGehlot You can't do it directly with
rbind()
and data.frame()
only. You could do something like setNames(stack(data.frame(t(rbind(boys_age, girls_age)))), c('age', 'group'))
which includes them, but are not explicit– Sotos
Nov 20 '18 at 10:00
@HimanshuGehlot You can't do it directly with
rbind()
and data.frame()
only. You could do something like setNames(stack(data.frame(t(rbind(boys_age, girls_age)))), c('age', 'group'))
which includes them, but are not explicit– Sotos
Nov 20 '18 at 10:00
|
show 7 more comments
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53389851%2ferror-in-match-namesclabs-namesxi-names-do-not-match-previous-names%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown