R reshape2 - transform 6 cols into 2 columns
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I am very new to R programming and stackoverflow, this is for a school's lab, please forgive me if I'm not asking the questions correctly..
I am given a data set with these columns:
ID | X_B_Rate | X_D_Rate | Y_B_Rate | Y_D_Rate | Z_B_Rate | Z_D_Rate
As you can see, the columns kind of repeat itself, the challenge is to write R code and transform the columns into:
ID | x_or_y | B_or_D | Rate
I have tried reshape2's melt() function, but I can't wrap my head around how RegExp works with it
test <- melt(df, id.vars=c('ID'), measure.vars=grep('X$',colnames(df)),
variable.name='x_or_y', value.name='Rate')
I know the code is definitely wrong... I'm only getting a list of IDs, I can't seem to understand how the RegExp works with the melt() function and I have tried googling "Reshape2 melt()" but everywhere there's only a very short doc on it.
I would totally appreciate if someone could tell me what am I doing wrong or at least point me in a the correct direction to the proper doc :(
Thanks in advance!
r reshape2
add a comment |
I am very new to R programming and stackoverflow, this is for a school's lab, please forgive me if I'm not asking the questions correctly..
I am given a data set with these columns:
ID | X_B_Rate | X_D_Rate | Y_B_Rate | Y_D_Rate | Z_B_Rate | Z_D_Rate
As you can see, the columns kind of repeat itself, the challenge is to write R code and transform the columns into:
ID | x_or_y | B_or_D | Rate
I have tried reshape2's melt() function, but I can't wrap my head around how RegExp works with it
test <- melt(df, id.vars=c('ID'), measure.vars=grep('X$',colnames(df)),
variable.name='x_or_y', value.name='Rate')
I know the code is definitely wrong... I'm only getting a list of IDs, I can't seem to understand how the RegExp works with the melt() function and I have tried googling "Reshape2 melt()" but everywhere there's only a very short doc on it.
I would totally appreciate if someone could tell me what am I doing wrong or at least point me in a the correct direction to the proper doc :(
Thanks in advance!
r reshape2
1
Could you provide sample data usingdput(yourdf)
for better results?
– NelsonGon
Nov 24 '18 at 17:12
add a comment |
I am very new to R programming and stackoverflow, this is for a school's lab, please forgive me if I'm not asking the questions correctly..
I am given a data set with these columns:
ID | X_B_Rate | X_D_Rate | Y_B_Rate | Y_D_Rate | Z_B_Rate | Z_D_Rate
As you can see, the columns kind of repeat itself, the challenge is to write R code and transform the columns into:
ID | x_or_y | B_or_D | Rate
I have tried reshape2's melt() function, but I can't wrap my head around how RegExp works with it
test <- melt(df, id.vars=c('ID'), measure.vars=grep('X$',colnames(df)),
variable.name='x_or_y', value.name='Rate')
I know the code is definitely wrong... I'm only getting a list of IDs, I can't seem to understand how the RegExp works with the melt() function and I have tried googling "Reshape2 melt()" but everywhere there's only a very short doc on it.
I would totally appreciate if someone could tell me what am I doing wrong or at least point me in a the correct direction to the proper doc :(
Thanks in advance!
r reshape2
I am very new to R programming and stackoverflow, this is for a school's lab, please forgive me if I'm not asking the questions correctly..
I am given a data set with these columns:
ID | X_B_Rate | X_D_Rate | Y_B_Rate | Y_D_Rate | Z_B_Rate | Z_D_Rate
As you can see, the columns kind of repeat itself, the challenge is to write R code and transform the columns into:
ID | x_or_y | B_or_D | Rate
I have tried reshape2's melt() function, but I can't wrap my head around how RegExp works with it
test <- melt(df, id.vars=c('ID'), measure.vars=grep('X$',colnames(df)),
variable.name='x_or_y', value.name='Rate')
I know the code is definitely wrong... I'm only getting a list of IDs, I can't seem to understand how the RegExp works with the melt() function and I have tried googling "Reshape2 melt()" but everywhere there's only a very short doc on it.
I would totally appreciate if someone could tell me what am I doing wrong or at least point me in a the correct direction to the proper doc :(
Thanks in advance!
r reshape2
r reshape2
asked Nov 24 '18 at 16:55
RivRiv
2116
2116
1
Could you provide sample data usingdput(yourdf)
for better results?
– NelsonGon
Nov 24 '18 at 17:12
add a comment |
1
Could you provide sample data usingdput(yourdf)
for better results?
– NelsonGon
Nov 24 '18 at 17:12
1
1
Could you provide sample data using
dput(yourdf)
for better results?– NelsonGon
Nov 24 '18 at 17:12
Could you provide sample data using
dput(yourdf)
for better results?– NelsonGon
Nov 24 '18 at 17:12
add a comment |
1 Answer
1
active
oldest
votes
This should do it:
yourdf %>%
gather("id","value",1:ncol(.))
You can then rename your columns in whichever way you wish.
EDIT: Using reshape2
you could do something like this:
yourdf%>%
mutate(ID=row_number()) %>%
reshape2::melt(id.vars=c("ID"))
I've used my own data to exemplify as your question provides no sample data.
add a comment |
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%2f53460401%2fr-reshape2-transform-6-cols-into-2-columns%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
This should do it:
yourdf %>%
gather("id","value",1:ncol(.))
You can then rename your columns in whichever way you wish.
EDIT: Using reshape2
you could do something like this:
yourdf%>%
mutate(ID=row_number()) %>%
reshape2::melt(id.vars=c("ID"))
I've used my own data to exemplify as your question provides no sample data.
add a comment |
This should do it:
yourdf %>%
gather("id","value",1:ncol(.))
You can then rename your columns in whichever way you wish.
EDIT: Using reshape2
you could do something like this:
yourdf%>%
mutate(ID=row_number()) %>%
reshape2::melt(id.vars=c("ID"))
I've used my own data to exemplify as your question provides no sample data.
add a comment |
This should do it:
yourdf %>%
gather("id","value",1:ncol(.))
You can then rename your columns in whichever way you wish.
EDIT: Using reshape2
you could do something like this:
yourdf%>%
mutate(ID=row_number()) %>%
reshape2::melt(id.vars=c("ID"))
I've used my own data to exemplify as your question provides no sample data.
This should do it:
yourdf %>%
gather("id","value",1:ncol(.))
You can then rename your columns in whichever way you wish.
EDIT: Using reshape2
you could do something like this:
yourdf%>%
mutate(ID=row_number()) %>%
reshape2::melt(id.vars=c("ID"))
I've used my own data to exemplify as your question provides no sample data.
edited Nov 24 '18 at 17:17
answered Nov 24 '18 at 17:11
NelsonGonNelsonGon
4,2264834
4,2264834
add a comment |
add a comment |
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%2f53460401%2fr-reshape2-transform-6-cols-into-2-columns%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
1
Could you provide sample data using
dput(yourdf)
for better results?– NelsonGon
Nov 24 '18 at 17:12