R: How to make extra rows from a column?












3















I have a data-set of human hands, where currently a single person is defined as a single observation. I want to reshape dataframe to have hands as individual observations. I tried something with "dplyr" package and "gather" function but had no success at all.



So from this, where each person is on one row :



id Gender Age   Present_R    Present_L    Dominant
1 F 2 TRUE TRUE R
2 F 5 TRUE FALSE L
3 M 8 FALSE FALSE R


to this, where each hand is on one row:



id Gender Age   Hand    Present  Dominant
1 F 2 R TRUE TRUE
2 F 2 L TRUE FALSE
3 F 5 R TRUE FALSE
4 F 5 L FALSE TRUE
5 M 8 R FALSE TRUE
6 M 8 L FALSE FALSE


Note that hand dominance becomes logical.










share|improve this question























  • What's the logic behind Dominance?

    – NelsonGon
    Jan 1 at 21:08











  • "Dominance" means hand dominance. Each person has a dominant hand and if hands are separated then each hand will be dominant (TRUE) or nondominant (FALSE).

    – user2021713
    Jan 1 at 21:17






  • 3





    and for old times sake: r = reshape(dat, direction="long", varying=c("Present_R", "Present_L"), sep="_", timevar="hand"), then set Dominant to how you want r$Dominant <- r$Dominant == r$hand

    – user20650
    Jan 1 at 22:19
















3















I have a data-set of human hands, where currently a single person is defined as a single observation. I want to reshape dataframe to have hands as individual observations. I tried something with "dplyr" package and "gather" function but had no success at all.



So from this, where each person is on one row :



id Gender Age   Present_R    Present_L    Dominant
1 F 2 TRUE TRUE R
2 F 5 TRUE FALSE L
3 M 8 FALSE FALSE R


to this, where each hand is on one row:



id Gender Age   Hand    Present  Dominant
1 F 2 R TRUE TRUE
2 F 2 L TRUE FALSE
3 F 5 R TRUE FALSE
4 F 5 L FALSE TRUE
5 M 8 R FALSE TRUE
6 M 8 L FALSE FALSE


Note that hand dominance becomes logical.










share|improve this question























  • What's the logic behind Dominance?

    – NelsonGon
    Jan 1 at 21:08











  • "Dominance" means hand dominance. Each person has a dominant hand and if hands are separated then each hand will be dominant (TRUE) or nondominant (FALSE).

    – user2021713
    Jan 1 at 21:17






  • 3





    and for old times sake: r = reshape(dat, direction="long", varying=c("Present_R", "Present_L"), sep="_", timevar="hand"), then set Dominant to how you want r$Dominant <- r$Dominant == r$hand

    – user20650
    Jan 1 at 22:19














3












3








3








I have a data-set of human hands, where currently a single person is defined as a single observation. I want to reshape dataframe to have hands as individual observations. I tried something with "dplyr" package and "gather" function but had no success at all.



So from this, where each person is on one row :



id Gender Age   Present_R    Present_L    Dominant
1 F 2 TRUE TRUE R
2 F 5 TRUE FALSE L
3 M 8 FALSE FALSE R


to this, where each hand is on one row:



id Gender Age   Hand    Present  Dominant
1 F 2 R TRUE TRUE
2 F 2 L TRUE FALSE
3 F 5 R TRUE FALSE
4 F 5 L FALSE TRUE
5 M 8 R FALSE TRUE
6 M 8 L FALSE FALSE


Note that hand dominance becomes logical.










share|improve this question














I have a data-set of human hands, where currently a single person is defined as a single observation. I want to reshape dataframe to have hands as individual observations. I tried something with "dplyr" package and "gather" function but had no success at all.



So from this, where each person is on one row :



id Gender Age   Present_R    Present_L    Dominant
1 F 2 TRUE TRUE R
2 F 5 TRUE FALSE L
3 M 8 FALSE FALSE R


to this, where each hand is on one row:



id Gender Age   Hand    Present  Dominant
1 F 2 R TRUE TRUE
2 F 2 L TRUE FALSE
3 F 5 R TRUE FALSE
4 F 5 L FALSE TRUE
5 M 8 R FALSE TRUE
6 M 8 L FALSE FALSE


Note that hand dominance becomes logical.







r dataframe






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 1 at 21:01









user2021713user2021713

186




186













  • What's the logic behind Dominance?

    – NelsonGon
    Jan 1 at 21:08











  • "Dominance" means hand dominance. Each person has a dominant hand and if hands are separated then each hand will be dominant (TRUE) or nondominant (FALSE).

    – user2021713
    Jan 1 at 21:17






  • 3





    and for old times sake: r = reshape(dat, direction="long", varying=c("Present_R", "Present_L"), sep="_", timevar="hand"), then set Dominant to how you want r$Dominant <- r$Dominant == r$hand

    – user20650
    Jan 1 at 22:19



















  • What's the logic behind Dominance?

    – NelsonGon
    Jan 1 at 21:08











  • "Dominance" means hand dominance. Each person has a dominant hand and if hands are separated then each hand will be dominant (TRUE) or nondominant (FALSE).

    – user2021713
    Jan 1 at 21:17






  • 3





    and for old times sake: r = reshape(dat, direction="long", varying=c("Present_R", "Present_L"), sep="_", timevar="hand"), then set Dominant to how you want r$Dominant <- r$Dominant == r$hand

    – user20650
    Jan 1 at 22:19

















What's the logic behind Dominance?

– NelsonGon
Jan 1 at 21:08





What's the logic behind Dominance?

– NelsonGon
Jan 1 at 21:08













"Dominance" means hand dominance. Each person has a dominant hand and if hands are separated then each hand will be dominant (TRUE) or nondominant (FALSE).

– user2021713
Jan 1 at 21:17





"Dominance" means hand dominance. Each person has a dominant hand and if hands are separated then each hand will be dominant (TRUE) or nondominant (FALSE).

– user2021713
Jan 1 at 21:17




3




3





and for old times sake: r = reshape(dat, direction="long", varying=c("Present_R", "Present_L"), sep="_", timevar="hand"), then set Dominant to how you want r$Dominant <- r$Dominant == r$hand

– user20650
Jan 1 at 22:19





and for old times sake: r = reshape(dat, direction="long", varying=c("Present_R", "Present_L"), sep="_", timevar="hand"), then set Dominant to how you want r$Dominant <- r$Dominant == r$hand

– user20650
Jan 1 at 22:19












2 Answers
2






active

oldest

votes


















5














We can gather into 'long' format, arrange by 'id', then create the 'Dominant' by unlisting the 'Present' columns, 'Hand' by removing the substring of the 'Hand' column



library(tidyverse)
gather(df1, Hand, Present, Present_R:Present_L) %>%
arrange(id) %>%
mutate(Dominant = unlist(df1[c("Present_L", "Present_R")]),
id = row_number(),
Hand = str_remove(Hand, ".*_"))
# id Gender Age Dominant Hand Present
#1 1 F 2 TRUE R TRUE
#2 2 F 2 FALSE L TRUE
#3 3 F 5 FALSE R TRUE
#4 4 F 5 TRUE L FALSE
#5 5 M 8 TRUE R FALSE
#6 6 M 8 FALSE L FALSE


Based on the OP' comments, it seems like we need to compare the 'Dominant' with the 'Hand'



gather(df1, Hand, Present, Present_R:Present_L) %>%
arrange(id) %>%
mutate(id = row_number(),
Hand = str_remove(Hand, ".*_"),
Dominant = Dominant == Hand)
# id Gender Age Dominant Hand Present
#1 1 F 2 TRUE R TRUE
#2 2 F 2 FALSE L TRUE
#3 3 F 5 FALSE R TRUE
#4 4 F 5 TRUE L FALSE
#5 5 M 8 TRUE R FALSE
#6 6 M 8 FALSE L FALSE





share|improve this answer





















  • 1





    mutate function looks amazing!

    – Farah Nazifa
    Jan 1 at 21:41






  • 1





    Nice. What's the logic that led you to unlist?

    – NelsonGon
    Jan 1 at 22:17













  • Should row_number() function have an argument? For me it produces error: Error in rank(x, ties.method = "first", na.last = "keep") : argument "x" is missing, with no default

    – user2021713
    Jan 1 at 22:23






  • 2





    @NelsonGon Thanks. For me, I was initially checking the expected output. But now, by looking at the comments the OP made, I think it is more correct to have gather(df1, Hand, Present, Present_R:Present_L) %>%arrange(id) %>% mutate(id = row_number(), Hand = str_remove(Hand, ".*_"), Dominant = Dominant == Hand)

    – akrun
    Jan 1 at 22:24








  • 1





    @user2021713 No, it should work without any argument. I am using R 3.5.1 and dplyr_0.7.6

    – akrun
    Jan 1 at 22:27



















0














With a small data frame (i.e., few variables, regardless of the number of cases), "hand-coding" may be the easiest approach:



with(df, data.frame(id = c(id,id), Gender=c(Gender,Gender), Age=c(Age, Age), 
Hand = c(rep("R", nrow(df)), rep("L", nrow(df))),
Present = c(Present_R, Present_L),
Dominant = c(Dominant=="R", Dominant=="L")
))





share|improve this answer
























  • This solution almost seems to work, but for me for some reason Age will be incremented by 1 and Gender will be represented by numbers 1 and 2.

    – user2021713
    Jan 2 at 10:55






  • 1





    Well, I can't think of any other reason than Age and Gender being factors in your data frame. You can bypass this using as.numeric(as.character(df$Age)) and as.character(df$Gender)

    – lebatsnok
    Jan 2 at 11:26











  • Yes that was the reason. Thanks!

    – user2021713
    Jan 2 at 13:06











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


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998913%2fr-how-to-make-extra-rows-from-a-column%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









5














We can gather into 'long' format, arrange by 'id', then create the 'Dominant' by unlisting the 'Present' columns, 'Hand' by removing the substring of the 'Hand' column



library(tidyverse)
gather(df1, Hand, Present, Present_R:Present_L) %>%
arrange(id) %>%
mutate(Dominant = unlist(df1[c("Present_L", "Present_R")]),
id = row_number(),
Hand = str_remove(Hand, ".*_"))
# id Gender Age Dominant Hand Present
#1 1 F 2 TRUE R TRUE
#2 2 F 2 FALSE L TRUE
#3 3 F 5 FALSE R TRUE
#4 4 F 5 TRUE L FALSE
#5 5 M 8 TRUE R FALSE
#6 6 M 8 FALSE L FALSE


Based on the OP' comments, it seems like we need to compare the 'Dominant' with the 'Hand'



gather(df1, Hand, Present, Present_R:Present_L) %>%
arrange(id) %>%
mutate(id = row_number(),
Hand = str_remove(Hand, ".*_"),
Dominant = Dominant == Hand)
# id Gender Age Dominant Hand Present
#1 1 F 2 TRUE R TRUE
#2 2 F 2 FALSE L TRUE
#3 3 F 5 FALSE R TRUE
#4 4 F 5 TRUE L FALSE
#5 5 M 8 TRUE R FALSE
#6 6 M 8 FALSE L FALSE





share|improve this answer





















  • 1





    mutate function looks amazing!

    – Farah Nazifa
    Jan 1 at 21:41






  • 1





    Nice. What's the logic that led you to unlist?

    – NelsonGon
    Jan 1 at 22:17













  • Should row_number() function have an argument? For me it produces error: Error in rank(x, ties.method = "first", na.last = "keep") : argument "x" is missing, with no default

    – user2021713
    Jan 1 at 22:23






  • 2





    @NelsonGon Thanks. For me, I was initially checking the expected output. But now, by looking at the comments the OP made, I think it is more correct to have gather(df1, Hand, Present, Present_R:Present_L) %>%arrange(id) %>% mutate(id = row_number(), Hand = str_remove(Hand, ".*_"), Dominant = Dominant == Hand)

    – akrun
    Jan 1 at 22:24








  • 1





    @user2021713 No, it should work without any argument. I am using R 3.5.1 and dplyr_0.7.6

    – akrun
    Jan 1 at 22:27
















5














We can gather into 'long' format, arrange by 'id', then create the 'Dominant' by unlisting the 'Present' columns, 'Hand' by removing the substring of the 'Hand' column



library(tidyverse)
gather(df1, Hand, Present, Present_R:Present_L) %>%
arrange(id) %>%
mutate(Dominant = unlist(df1[c("Present_L", "Present_R")]),
id = row_number(),
Hand = str_remove(Hand, ".*_"))
# id Gender Age Dominant Hand Present
#1 1 F 2 TRUE R TRUE
#2 2 F 2 FALSE L TRUE
#3 3 F 5 FALSE R TRUE
#4 4 F 5 TRUE L FALSE
#5 5 M 8 TRUE R FALSE
#6 6 M 8 FALSE L FALSE


Based on the OP' comments, it seems like we need to compare the 'Dominant' with the 'Hand'



gather(df1, Hand, Present, Present_R:Present_L) %>%
arrange(id) %>%
mutate(id = row_number(),
Hand = str_remove(Hand, ".*_"),
Dominant = Dominant == Hand)
# id Gender Age Dominant Hand Present
#1 1 F 2 TRUE R TRUE
#2 2 F 2 FALSE L TRUE
#3 3 F 5 FALSE R TRUE
#4 4 F 5 TRUE L FALSE
#5 5 M 8 TRUE R FALSE
#6 6 M 8 FALSE L FALSE





share|improve this answer





















  • 1





    mutate function looks amazing!

    – Farah Nazifa
    Jan 1 at 21:41






  • 1





    Nice. What's the logic that led you to unlist?

    – NelsonGon
    Jan 1 at 22:17













  • Should row_number() function have an argument? For me it produces error: Error in rank(x, ties.method = "first", na.last = "keep") : argument "x" is missing, with no default

    – user2021713
    Jan 1 at 22:23






  • 2





    @NelsonGon Thanks. For me, I was initially checking the expected output. But now, by looking at the comments the OP made, I think it is more correct to have gather(df1, Hand, Present, Present_R:Present_L) %>%arrange(id) %>% mutate(id = row_number(), Hand = str_remove(Hand, ".*_"), Dominant = Dominant == Hand)

    – akrun
    Jan 1 at 22:24








  • 1





    @user2021713 No, it should work without any argument. I am using R 3.5.1 and dplyr_0.7.6

    – akrun
    Jan 1 at 22:27














5












5








5







We can gather into 'long' format, arrange by 'id', then create the 'Dominant' by unlisting the 'Present' columns, 'Hand' by removing the substring of the 'Hand' column



library(tidyverse)
gather(df1, Hand, Present, Present_R:Present_L) %>%
arrange(id) %>%
mutate(Dominant = unlist(df1[c("Present_L", "Present_R")]),
id = row_number(),
Hand = str_remove(Hand, ".*_"))
# id Gender Age Dominant Hand Present
#1 1 F 2 TRUE R TRUE
#2 2 F 2 FALSE L TRUE
#3 3 F 5 FALSE R TRUE
#4 4 F 5 TRUE L FALSE
#5 5 M 8 TRUE R FALSE
#6 6 M 8 FALSE L FALSE


Based on the OP' comments, it seems like we need to compare the 'Dominant' with the 'Hand'



gather(df1, Hand, Present, Present_R:Present_L) %>%
arrange(id) %>%
mutate(id = row_number(),
Hand = str_remove(Hand, ".*_"),
Dominant = Dominant == Hand)
# id Gender Age Dominant Hand Present
#1 1 F 2 TRUE R TRUE
#2 2 F 2 FALSE L TRUE
#3 3 F 5 FALSE R TRUE
#4 4 F 5 TRUE L FALSE
#5 5 M 8 TRUE R FALSE
#6 6 M 8 FALSE L FALSE





share|improve this answer















We can gather into 'long' format, arrange by 'id', then create the 'Dominant' by unlisting the 'Present' columns, 'Hand' by removing the substring of the 'Hand' column



library(tidyverse)
gather(df1, Hand, Present, Present_R:Present_L) %>%
arrange(id) %>%
mutate(Dominant = unlist(df1[c("Present_L", "Present_R")]),
id = row_number(),
Hand = str_remove(Hand, ".*_"))
# id Gender Age Dominant Hand Present
#1 1 F 2 TRUE R TRUE
#2 2 F 2 FALSE L TRUE
#3 3 F 5 FALSE R TRUE
#4 4 F 5 TRUE L FALSE
#5 5 M 8 TRUE R FALSE
#6 6 M 8 FALSE L FALSE


Based on the OP' comments, it seems like we need to compare the 'Dominant' with the 'Hand'



gather(df1, Hand, Present, Present_R:Present_L) %>%
arrange(id) %>%
mutate(id = row_number(),
Hand = str_remove(Hand, ".*_"),
Dominant = Dominant == Hand)
# id Gender Age Dominant Hand Present
#1 1 F 2 TRUE R TRUE
#2 2 F 2 FALSE L TRUE
#3 3 F 5 FALSE R TRUE
#4 4 F 5 TRUE L FALSE
#5 5 M 8 TRUE R FALSE
#6 6 M 8 FALSE L FALSE






share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 1 at 22:25

























answered Jan 1 at 21:21









akrunakrun

414k13201275




414k13201275








  • 1





    mutate function looks amazing!

    – Farah Nazifa
    Jan 1 at 21:41






  • 1





    Nice. What's the logic that led you to unlist?

    – NelsonGon
    Jan 1 at 22:17













  • Should row_number() function have an argument? For me it produces error: Error in rank(x, ties.method = "first", na.last = "keep") : argument "x" is missing, with no default

    – user2021713
    Jan 1 at 22:23






  • 2





    @NelsonGon Thanks. For me, I was initially checking the expected output. But now, by looking at the comments the OP made, I think it is more correct to have gather(df1, Hand, Present, Present_R:Present_L) %>%arrange(id) %>% mutate(id = row_number(), Hand = str_remove(Hand, ".*_"), Dominant = Dominant == Hand)

    – akrun
    Jan 1 at 22:24








  • 1





    @user2021713 No, it should work without any argument. I am using R 3.5.1 and dplyr_0.7.6

    – akrun
    Jan 1 at 22:27














  • 1





    mutate function looks amazing!

    – Farah Nazifa
    Jan 1 at 21:41






  • 1





    Nice. What's the logic that led you to unlist?

    – NelsonGon
    Jan 1 at 22:17













  • Should row_number() function have an argument? For me it produces error: Error in rank(x, ties.method = "first", na.last = "keep") : argument "x" is missing, with no default

    – user2021713
    Jan 1 at 22:23






  • 2





    @NelsonGon Thanks. For me, I was initially checking the expected output. But now, by looking at the comments the OP made, I think it is more correct to have gather(df1, Hand, Present, Present_R:Present_L) %>%arrange(id) %>% mutate(id = row_number(), Hand = str_remove(Hand, ".*_"), Dominant = Dominant == Hand)

    – akrun
    Jan 1 at 22:24








  • 1





    @user2021713 No, it should work without any argument. I am using R 3.5.1 and dplyr_0.7.6

    – akrun
    Jan 1 at 22:27








1




1





mutate function looks amazing!

– Farah Nazifa
Jan 1 at 21:41





mutate function looks amazing!

– Farah Nazifa
Jan 1 at 21:41




1




1





Nice. What's the logic that led you to unlist?

– NelsonGon
Jan 1 at 22:17







Nice. What's the logic that led you to unlist?

– NelsonGon
Jan 1 at 22:17















Should row_number() function have an argument? For me it produces error: Error in rank(x, ties.method = "first", na.last = "keep") : argument "x" is missing, with no default

– user2021713
Jan 1 at 22:23





Should row_number() function have an argument? For me it produces error: Error in rank(x, ties.method = "first", na.last = "keep") : argument "x" is missing, with no default

– user2021713
Jan 1 at 22:23




2




2





@NelsonGon Thanks. For me, I was initially checking the expected output. But now, by looking at the comments the OP made, I think it is more correct to have gather(df1, Hand, Present, Present_R:Present_L) %>%arrange(id) %>% mutate(id = row_number(), Hand = str_remove(Hand, ".*_"), Dominant = Dominant == Hand)

– akrun
Jan 1 at 22:24







@NelsonGon Thanks. For me, I was initially checking the expected output. But now, by looking at the comments the OP made, I think it is more correct to have gather(df1, Hand, Present, Present_R:Present_L) %>%arrange(id) %>% mutate(id = row_number(), Hand = str_remove(Hand, ".*_"), Dominant = Dominant == Hand)

– akrun
Jan 1 at 22:24






1




1





@user2021713 No, it should work without any argument. I am using R 3.5.1 and dplyr_0.7.6

– akrun
Jan 1 at 22:27





@user2021713 No, it should work without any argument. I am using R 3.5.1 and dplyr_0.7.6

– akrun
Jan 1 at 22:27













0














With a small data frame (i.e., few variables, regardless of the number of cases), "hand-coding" may be the easiest approach:



with(df, data.frame(id = c(id,id), Gender=c(Gender,Gender), Age=c(Age, Age), 
Hand = c(rep("R", nrow(df)), rep("L", nrow(df))),
Present = c(Present_R, Present_L),
Dominant = c(Dominant=="R", Dominant=="L")
))





share|improve this answer
























  • This solution almost seems to work, but for me for some reason Age will be incremented by 1 and Gender will be represented by numbers 1 and 2.

    – user2021713
    Jan 2 at 10:55






  • 1





    Well, I can't think of any other reason than Age and Gender being factors in your data frame. You can bypass this using as.numeric(as.character(df$Age)) and as.character(df$Gender)

    – lebatsnok
    Jan 2 at 11:26











  • Yes that was the reason. Thanks!

    – user2021713
    Jan 2 at 13:06
















0














With a small data frame (i.e., few variables, regardless of the number of cases), "hand-coding" may be the easiest approach:



with(df, data.frame(id = c(id,id), Gender=c(Gender,Gender), Age=c(Age, Age), 
Hand = c(rep("R", nrow(df)), rep("L", nrow(df))),
Present = c(Present_R, Present_L),
Dominant = c(Dominant=="R", Dominant=="L")
))





share|improve this answer
























  • This solution almost seems to work, but for me for some reason Age will be incremented by 1 and Gender will be represented by numbers 1 and 2.

    – user2021713
    Jan 2 at 10:55






  • 1





    Well, I can't think of any other reason than Age and Gender being factors in your data frame. You can bypass this using as.numeric(as.character(df$Age)) and as.character(df$Gender)

    – lebatsnok
    Jan 2 at 11:26











  • Yes that was the reason. Thanks!

    – user2021713
    Jan 2 at 13:06














0












0








0







With a small data frame (i.e., few variables, regardless of the number of cases), "hand-coding" may be the easiest approach:



with(df, data.frame(id = c(id,id), Gender=c(Gender,Gender), Age=c(Age, Age), 
Hand = c(rep("R", nrow(df)), rep("L", nrow(df))),
Present = c(Present_R, Present_L),
Dominant = c(Dominant=="R", Dominant=="L")
))





share|improve this answer













With a small data frame (i.e., few variables, regardless of the number of cases), "hand-coding" may be the easiest approach:



with(df, data.frame(id = c(id,id), Gender=c(Gender,Gender), Age=c(Age, Age), 
Hand = c(rep("R", nrow(df)), rep("L", nrow(df))),
Present = c(Present_R, Present_L),
Dominant = c(Dominant=="R", Dominant=="L")
))






share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 1 at 21:28









lebatsnoklebatsnok

4,42621118




4,42621118













  • This solution almost seems to work, but for me for some reason Age will be incremented by 1 and Gender will be represented by numbers 1 and 2.

    – user2021713
    Jan 2 at 10:55






  • 1





    Well, I can't think of any other reason than Age and Gender being factors in your data frame. You can bypass this using as.numeric(as.character(df$Age)) and as.character(df$Gender)

    – lebatsnok
    Jan 2 at 11:26











  • Yes that was the reason. Thanks!

    – user2021713
    Jan 2 at 13:06



















  • This solution almost seems to work, but for me for some reason Age will be incremented by 1 and Gender will be represented by numbers 1 and 2.

    – user2021713
    Jan 2 at 10:55






  • 1





    Well, I can't think of any other reason than Age and Gender being factors in your data frame. You can bypass this using as.numeric(as.character(df$Age)) and as.character(df$Gender)

    – lebatsnok
    Jan 2 at 11:26











  • Yes that was the reason. Thanks!

    – user2021713
    Jan 2 at 13:06

















This solution almost seems to work, but for me for some reason Age will be incremented by 1 and Gender will be represented by numbers 1 and 2.

– user2021713
Jan 2 at 10:55





This solution almost seems to work, but for me for some reason Age will be incremented by 1 and Gender will be represented by numbers 1 and 2.

– user2021713
Jan 2 at 10:55




1




1





Well, I can't think of any other reason than Age and Gender being factors in your data frame. You can bypass this using as.numeric(as.character(df$Age)) and as.character(df$Gender)

– lebatsnok
Jan 2 at 11:26





Well, I can't think of any other reason than Age and Gender being factors in your data frame. You can bypass this using as.numeric(as.character(df$Age)) and as.character(df$Gender)

– lebatsnok
Jan 2 at 11:26













Yes that was the reason. Thanks!

– user2021713
Jan 2 at 13:06





Yes that was the reason. Thanks!

– user2021713
Jan 2 at 13:06


















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53998913%2fr-how-to-make-extra-rows-from-a-column%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

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

How to fix TextFormField cause rebuild widget in Flutter