Rearranging data frame from column names to key value pairs Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm 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
Why are two-digit numbers in Jonathan Swift's "Gulliver's Travels" (1726) written in "German style"?
Why did Bronn offer to be Tyrion Lannister's champion in trial by combat?
The Nth Gryphon Number
Vertical ranges of Column Plots in 12
How does the body cool itself in a stillsuit?
Russian equivalents of おしゃれは足元から (Every good outfit starts with the shoes)
Diophantine equation 3^a+1=3^b+5^c
Why is there so little support for joining EFTA in the British parliament?
Flight departed from the gate 5 min before scheduled departure time. Refund options
What are some likely causes to domain member PC losing contact to domain controller?
Does the main washing effect of soap come from foam?
How do I say "this must not happen"?
Twin's vs. Twins'
How could a hydrazine and N2O4 cloud (or it's reactants) show up in weather radar?
What is "Lambda" in Heston's original paper on stochastic volatility models?
Can two people see the same photon?
Is there a spell that can create a permanent fire?
Does the universe have a fixed centre of mass?
Why are current probes so expensive?
Centre cell vertically in tabularx
malloc in main() or malloc in another function: allocating memory for a struct and its members
What did Turing mean when saying that "machines cannot give rise to surprises" is due to a fallacy?
What is a more techy Technical Writer job title that isn't cutesy or confusing?
Adapting the Chinese Remainder Theorem (CRT) for integers to polynomials
Rearranging data frame from column names to key value pairs
Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm 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