Rearranging data frame from column names to key value pairs Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) 2019 Moderator Election Q&A - Questionnaire 2019 Community Moderator Election Resultssubsetting R data frameData frame mutation in RHow to cluster multiple time-series from one data frameHow to plot two distribution curves in a faceted way in R/ggplot2?Retrieving column names in RSlice rows in R based on column valueHow do i convert a Named num [1:4] to a data frame in R?How can I sort a data frame by groups?R - How do I remove a varying number of digits from a date-vectorExtract or subset hundreds of columns from a data frame
Project Euler #1 in C++
Why wasn't DOSKEY integrated with COMMAND.COM?
What is the meaning of the simile “quick as silk”?
How fail-safe is nr as stop bytes?
How to convince students of the implication truth values?
Can family of EU Blue Card holder travel freely in the Schengen Area with a German Aufenthaltstitel?
How to react to hostile behavior from a senior developer?
Is there any word for a place full of confusion?
Would "destroying" Wurmcoil Engine prevent its tokens from being created?
What do you call the main part of a joke?
AppleTVs create a chatty alternate WiFi network
Dating a Former Employee
Century handling in Pandas
Is the Standard Deduction better than Itemized when both are the same amount?
Does the Weapon Master feat grant you a fighting style?
What causes the direction of lightning flashes?
Can a new player join a group only when a new campaign starts?
Denied boarding although I have proper visa and documentation. To whom should I make a complaint?
Is this homebrew Lady of Pain warlock patron balanced?
Selecting user stories during sprint planning
What font is "z" in "z-score"?
Is it a good idea to use CNN to classify 1D signal?
Amount of permutations on an NxNxN Rubik's Cube
How to tell that you are a giant?
Rearranging data frame from column names to key value pairs
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
2019 Moderator Election Q&A - Questionnaire
2019 Community Moderator Election Resultssubsetting R data frameData frame mutation in RHow to cluster multiple time-series from one data frameHow to plot two distribution curves in a faceted way in R/ggplot2?Retrieving column names in RSlice rows in R based on column valueHow do i convert a Named num [1:4] to a data frame in R?How can I sort a data frame by groups?R - How do I remove a varying number of digits from a date-vectorExtract or subset hundreds of columns from a data frame
$begingroup$
If I have data like this in R:
> a = data.frame(a = c(1, 2),
b = c(3, 4))
> a
a b
1 1 3
2 2 4
but I would like to have it like this:
> b = data.frame(k = c("a", "a", "b", "b"),
+ v = c(1, 2, 3, 4))
> b
k v
1 a 1
2 a 2
3 b 3
4 b 4
How can I convert the data this way?
r data
$endgroup$
add a comment |
$begingroup$
If I have data like this in R:
> a = data.frame(a = c(1, 2),
b = c(3, 4))
> a
a b
1 1 3
2 2 4
but I would like to have it like this:
> b = data.frame(k = c("a", "a", "b", "b"),
+ v = c(1, 2, 3, 4))
> b
k v
1 a 1
2 a 2
3 b 3
4 b 4
How can I convert the data this way?
r data
$endgroup$
add a comment |
$begingroup$
If I have data like this in R:
> a = data.frame(a = c(1, 2),
b = c(3, 4))
> a
a b
1 1 3
2 2 4
but I would like to have it like this:
> b = data.frame(k = c("a", "a", "b", "b"),
+ v = c(1, 2, 3, 4))
> b
k v
1 a 1
2 a 2
3 b 3
4 b 4
How can I convert the data this way?
r data
$endgroup$
If I have data like this in R:
> a = data.frame(a = c(1, 2),
b = c(3, 4))
> a
a b
1 1 3
2 2 4
but I would like to have it like this:
> b = data.frame(k = c("a", "a", "b", "b"),
+ v = c(1, 2, 3, 4))
> b
k v
1 a 1
2 a 2
3 b 3
4 b 4
How can I convert the data this way?
r data
r data
edited Apr 4 at 14:06
Stephen Rauch♦
1,52551330
1,52551330
asked Apr 4 at 13:14
MartinMartin
183
183
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
This is a simple gather()
using package tidyr
:
> library(tidyr)
>
> gather(a, key = "k", value = "v")
k v
1 a 1
2 a 2
3 b 3
4 b 4
$endgroup$
add a comment |
$begingroup$
You can alternatively use the melt()
function from the reshape2
package
> library(reshape2)
>
> melt(a, variable.name="k", value.name="v")
No id variables; using all as measure variables
k v
1 a 1
2 a 2
3 b 3
4 b 4
Hope this is insightful!
$endgroup$
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "557"
;
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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%2fdatascience.stackexchange.com%2fquestions%2f48609%2frearranging-data-frame-from-column-names-to-key-value-pairs%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
$begingroup$
This is a simple gather()
using package tidyr
:
> library(tidyr)
>
> gather(a, key = "k", value = "v")
k v
1 a 1
2 a 2
3 b 3
4 b 4
$endgroup$
add a comment |
$begingroup$
This is a simple gather()
using package tidyr
:
> library(tidyr)
>
> gather(a, key = "k", value = "v")
k v
1 a 1
2 a 2
3 b 3
4 b 4
$endgroup$
add a comment |
$begingroup$
This is a simple gather()
using package tidyr
:
> library(tidyr)
>
> gather(a, key = "k", value = "v")
k v
1 a 1
2 a 2
3 b 3
4 b 4
$endgroup$
This is a simple gather()
using package tidyr
:
> library(tidyr)
>
> gather(a, key = "k", value = "v")
k v
1 a 1
2 a 2
3 b 3
4 b 4
answered Apr 4 at 15:09
AdamAdam
1363
1363
add a comment |
add a comment |
$begingroup$
You can alternatively use the melt()
function from the reshape2
package
> library(reshape2)
>
> melt(a, variable.name="k", value.name="v")
No id variables; using all as measure variables
k v
1 a 1
2 a 2
3 b 3
4 b 4
Hope this is insightful!
$endgroup$
add a comment |
$begingroup$
You can alternatively use the melt()
function from the reshape2
package
> library(reshape2)
>
> melt(a, variable.name="k", value.name="v")
No id variables; using all as measure variables
k v
1 a 1
2 a 2
3 b 3
4 b 4
Hope this is insightful!
$endgroup$
add a comment |
$begingroup$
You can alternatively use the melt()
function from the reshape2
package
> library(reshape2)
>
> melt(a, variable.name="k", value.name="v")
No id variables; using all as measure variables
k v
1 a 1
2 a 2
3 b 3
4 b 4
Hope this is insightful!
$endgroup$
You can alternatively use the melt()
function from the reshape2
package
> library(reshape2)
>
> melt(a, variable.name="k", value.name="v")
No id variables; using all as measure variables
k v
1 a 1
2 a 2
3 b 3
4 b 4
Hope this is insightful!
answered Apr 4 at 15:40
I'mAnAccountantIKnowAlotOfMathI'mAnAccountantIKnowAlotOfMath
1213
1213
add a comment |
add a comment |
Thanks for contributing an answer to Data Science Stack Exchange!
- 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.
Use MathJax to format equations. MathJax reference.
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%2fdatascience.stackexchange.com%2fquestions%2f48609%2frearranging-data-frame-from-column-names-to-key-value-pairs%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