How to stack a dataframe in R [duplicate]
up vote
2
down vote
favorite
This question already has an answer here:
Reshaping data.frame from wide to long format
5 answers
I have a data frame that I would like to stack in R so that I end up with three columns. Below cis some example data in its current format.
> dput(df)
structure(list(Day = c("d1", "d2", "d3", "d4", "d5", "d6", "d7",
"d8", "d9", "d10"), A1 = c(14L, 24L, 22L, NA, NA, NA, NA, NA,
NA, NA), A2 = c(9L, 15L, 34L, 2L, 12L, 34L, 234L, 34L, NA, NA
), A3 = c(3L, 4L, 19L, 76L, 34L, 34L, 23L, 24L, 37L, 44L), A1.1 = c(34L,
55L, 75L, 12L, 56L, 35L, 3L, 22L, NA, NA)), .Names = c("Day",
"A1", "A2", "A3", "A1.1"), class = "data.frame", row.names = c(NA,
-10L))
> df
Day A1 A2 A3 A1.1
1 d1 14 9 3 34
2 d2 24 15 4 55
3 d3 22 34 19 75
4 d4 NA 2 76 12
5 d5 NA 12 34 56
6 d6 NA 34 34 35
7 d7 NA 234 23 3
8 d8 NA 34 24 22
9 d9 NA NA 37 NA
10 d10 NA NA 44 NA
I would like to end up with the dataframe formatted like so with three columns entitles "Day", "Animal" and "Count":
Day Animal Count
d1 A1 14
d2 A1 24
d3 A1 22
d4 A1 NA
d5 A1 NA
d6 A1 NA
d7 A1 NA
d8 A1 NA
d9 A1 NA
d10 A1 NA
d1 A2 9
d2 A2 15
d3 A2 34
d4 A2 2
d5 A2 12
d6 A2 34
d7 A2 234
d8 A2 34
d9 A2 NA
d10 A2 NA
d1 A3 3
d2 A3 4
d3 A3 19
d4 A3 76
d5 A3 34
d6 A3 34
d7 A3 23
d8 A3 24
d9 A3 37
d10 A3 44
d1 A1 34
d2 A1 55
d3 A1 75
d4 A1 12
d5 A1 56
d6 A1 35
d7 A1 3
d8 A1 22
d9 A1 NA
d10 A1 NA
I know that this should be an easy task but I am really struggling to find the solution. Any help with be much appreciated.
r dataframe stack reformat
marked as duplicate by zx8754
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
2 days ago
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
up vote
2
down vote
favorite
This question already has an answer here:
Reshaping data.frame from wide to long format
5 answers
I have a data frame that I would like to stack in R so that I end up with three columns. Below cis some example data in its current format.
> dput(df)
structure(list(Day = c("d1", "d2", "d3", "d4", "d5", "d6", "d7",
"d8", "d9", "d10"), A1 = c(14L, 24L, 22L, NA, NA, NA, NA, NA,
NA, NA), A2 = c(9L, 15L, 34L, 2L, 12L, 34L, 234L, 34L, NA, NA
), A3 = c(3L, 4L, 19L, 76L, 34L, 34L, 23L, 24L, 37L, 44L), A1.1 = c(34L,
55L, 75L, 12L, 56L, 35L, 3L, 22L, NA, NA)), .Names = c("Day",
"A1", "A2", "A3", "A1.1"), class = "data.frame", row.names = c(NA,
-10L))
> df
Day A1 A2 A3 A1.1
1 d1 14 9 3 34
2 d2 24 15 4 55
3 d3 22 34 19 75
4 d4 NA 2 76 12
5 d5 NA 12 34 56
6 d6 NA 34 34 35
7 d7 NA 234 23 3
8 d8 NA 34 24 22
9 d9 NA NA 37 NA
10 d10 NA NA 44 NA
I would like to end up with the dataframe formatted like so with three columns entitles "Day", "Animal" and "Count":
Day Animal Count
d1 A1 14
d2 A1 24
d3 A1 22
d4 A1 NA
d5 A1 NA
d6 A1 NA
d7 A1 NA
d8 A1 NA
d9 A1 NA
d10 A1 NA
d1 A2 9
d2 A2 15
d3 A2 34
d4 A2 2
d5 A2 12
d6 A2 34
d7 A2 234
d8 A2 34
d9 A2 NA
d10 A2 NA
d1 A3 3
d2 A3 4
d3 A3 19
d4 A3 76
d5 A3 34
d6 A3 34
d7 A3 23
d8 A3 24
d9 A3 37
d10 A3 44
d1 A1 34
d2 A1 55
d3 A1 75
d4 A1 12
d5 A1 56
d6 A1 35
d7 A1 3
d8 A1 22
d9 A1 NA
d10 A1 NA
I know that this should be an easy task but I am really struggling to find the solution. Any help with be much appreciated.
r dataframe stack reformat
marked as duplicate by zx8754
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
2 days ago
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
This question already has an answer here:
Reshaping data.frame from wide to long format
5 answers
I have a data frame that I would like to stack in R so that I end up with three columns. Below cis some example data in its current format.
> dput(df)
structure(list(Day = c("d1", "d2", "d3", "d4", "d5", "d6", "d7",
"d8", "d9", "d10"), A1 = c(14L, 24L, 22L, NA, NA, NA, NA, NA,
NA, NA), A2 = c(9L, 15L, 34L, 2L, 12L, 34L, 234L, 34L, NA, NA
), A3 = c(3L, 4L, 19L, 76L, 34L, 34L, 23L, 24L, 37L, 44L), A1.1 = c(34L,
55L, 75L, 12L, 56L, 35L, 3L, 22L, NA, NA)), .Names = c("Day",
"A1", "A2", "A3", "A1.1"), class = "data.frame", row.names = c(NA,
-10L))
> df
Day A1 A2 A3 A1.1
1 d1 14 9 3 34
2 d2 24 15 4 55
3 d3 22 34 19 75
4 d4 NA 2 76 12
5 d5 NA 12 34 56
6 d6 NA 34 34 35
7 d7 NA 234 23 3
8 d8 NA 34 24 22
9 d9 NA NA 37 NA
10 d10 NA NA 44 NA
I would like to end up with the dataframe formatted like so with three columns entitles "Day", "Animal" and "Count":
Day Animal Count
d1 A1 14
d2 A1 24
d3 A1 22
d4 A1 NA
d5 A1 NA
d6 A1 NA
d7 A1 NA
d8 A1 NA
d9 A1 NA
d10 A1 NA
d1 A2 9
d2 A2 15
d3 A2 34
d4 A2 2
d5 A2 12
d6 A2 34
d7 A2 234
d8 A2 34
d9 A2 NA
d10 A2 NA
d1 A3 3
d2 A3 4
d3 A3 19
d4 A3 76
d5 A3 34
d6 A3 34
d7 A3 23
d8 A3 24
d9 A3 37
d10 A3 44
d1 A1 34
d2 A1 55
d3 A1 75
d4 A1 12
d5 A1 56
d6 A1 35
d7 A1 3
d8 A1 22
d9 A1 NA
d10 A1 NA
I know that this should be an easy task but I am really struggling to find the solution. Any help with be much appreciated.
r dataframe stack reformat
This question already has an answer here:
Reshaping data.frame from wide to long format
5 answers
I have a data frame that I would like to stack in R so that I end up with three columns. Below cis some example data in its current format.
> dput(df)
structure(list(Day = c("d1", "d2", "d3", "d4", "d5", "d6", "d7",
"d8", "d9", "d10"), A1 = c(14L, 24L, 22L, NA, NA, NA, NA, NA,
NA, NA), A2 = c(9L, 15L, 34L, 2L, 12L, 34L, 234L, 34L, NA, NA
), A3 = c(3L, 4L, 19L, 76L, 34L, 34L, 23L, 24L, 37L, 44L), A1.1 = c(34L,
55L, 75L, 12L, 56L, 35L, 3L, 22L, NA, NA)), .Names = c("Day",
"A1", "A2", "A3", "A1.1"), class = "data.frame", row.names = c(NA,
-10L))
> df
Day A1 A2 A3 A1.1
1 d1 14 9 3 34
2 d2 24 15 4 55
3 d3 22 34 19 75
4 d4 NA 2 76 12
5 d5 NA 12 34 56
6 d6 NA 34 34 35
7 d7 NA 234 23 3
8 d8 NA 34 24 22
9 d9 NA NA 37 NA
10 d10 NA NA 44 NA
I would like to end up with the dataframe formatted like so with three columns entitles "Day", "Animal" and "Count":
Day Animal Count
d1 A1 14
d2 A1 24
d3 A1 22
d4 A1 NA
d5 A1 NA
d6 A1 NA
d7 A1 NA
d8 A1 NA
d9 A1 NA
d10 A1 NA
d1 A2 9
d2 A2 15
d3 A2 34
d4 A2 2
d5 A2 12
d6 A2 34
d7 A2 234
d8 A2 34
d9 A2 NA
d10 A2 NA
d1 A3 3
d2 A3 4
d3 A3 19
d4 A3 76
d5 A3 34
d6 A3 34
d7 A3 23
d8 A3 24
d9 A3 37
d10 A3 44
d1 A1 34
d2 A1 55
d3 A1 75
d4 A1 12
d5 A1 56
d6 A1 35
d7 A1 3
d8 A1 22
d9 A1 NA
d10 A1 NA
I know that this should be an easy task but I am really struggling to find the solution. Any help with be much appreciated.
This question already has an answer here:
Reshaping data.frame from wide to long format
5 answers
r dataframe stack reformat
r dataframe stack reformat
edited 2 days ago
asked 2 days ago
jjulip
5022919
5022919
marked as duplicate by zx8754
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
2 days ago
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by zx8754
StackExchange.ready(function()
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function()
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function()
$hover.showInfoMessage('',
messageElement: $msg.clone().show(),
transient: false,
position: my: 'bottom left', at: 'top center', offsetTop: -7 ,
dismissable: false,
relativeToBody: true
);
,
function()
StackExchange.helpers.removeMessages();
);
);
);
2 days ago
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
up vote
5
down vote
With dplyr and tidyr:
library(dplyr)
library(tidyr)
df <- df %>%
gather("animal", "count", -Day)
head(df)
# Day animal count
# 1 d1 A1 14
# 2 d2 A1 24
# 3 d3 A1 22
# 4 d4 A1 NA
# 5 d5 A1 NA
# 6 d6 A1 NA
1
Amazing! Thank you so much! Appreciated. Could you add library(tidyr) to your answer for the gather function please?
– jjulip
2 days ago
I have updated my question as I have a large data frame with multiple blocks of data for the same animal and Days but different values and your solution does not work in this case. Any suggestions? This is the error message that I get: Error: Can't bind data because some arguments have the same name Callrlang::last_error()to see a backtrace
– jjulip
2 days ago
It is working for me even with the new data, try cleaning your workspace first then retry
– RLave
2 days ago
No. I've tried multiple times with multiple examples and I get the same error every time :(
– jjulip
2 days ago
which version ofdplyrandtidyrdo you have? SeepackageVersion("dplyr"). Try update to the last version by reinstalling both:devtools::install_github("hadley/dplyr")orinstall_packages("dplyr").
– RLave
2 days ago
add a comment |
up vote
2
down vote
It can be done with base R function melt:
melt(df)
OUTPUT :
Day variable value
1 d1 A1 14
2 d2 A1 24
3 d3 A1 22
4 d4 A1 NA
5 d5 A1 NA
6 d6 A1 NA
7 d7 A1 NA
8 d8 A1 NA
9 d9 A1 NA
10 d10 A1 NA
11 d1 A2 9
12 d2 A2 15
13 d3 A2 34
14 d4 A2 2
15 d5 A2 12
16 d6 A2 34
17 d7 A2 234
18 d8 A2 34
19 d9 A2 NA
20 d10 A2 NA
21 d1 A3 3
22 d2 A3 4
23 d3 A3 19
24 d4 A3 76
25 d5 A3 34
26 d6 A3 34
27 d7 A3 23
28 d8 A3 24
29 d9 A3 37
30 d10 A3 44
add a comment |
up vote
2
down vote
You want to format your data frame from a wide format to a long format using the melt function of the reshape 2 package also answered here
library(reshape2)
df <- structure(list(Day = c("d1", "d2", "d3", "d4", "d5", "d6", "d7",
"d8", "d9", "d10"), A1 = c(14L, 24L, 22L, NA, NA, NA, NA, NA,
NA, NA), A2 = c(9L, 15L, 34L, 2L, 12L, 34L, 234L, 34L, NA, NA
), A3 = c(3L, 4L, 19L, 76L, 34L, 34L, 23L, 24L, 37L, 44L)), .Names = c("Day",
"A1", "A2", "A3"), class = "data.frame", row.names = c(NA, -10L
))
long_format <- melt(df)
colnames(long_format)[2:3] <- c("Animal","Count")
New contributor
seth-1 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
How does melt determine what the id variable is? Or how would I specify this? In my huge dataset it is having trouble doing this.
– jjulip
2 days ago
@jjulip you can try?melt.data.frameto get more information on the function. See e.g. under Arguments and id.vars.
– seth-1
2 days ago
add a comment |
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
With dplyr and tidyr:
library(dplyr)
library(tidyr)
df <- df %>%
gather("animal", "count", -Day)
head(df)
# Day animal count
# 1 d1 A1 14
# 2 d2 A1 24
# 3 d3 A1 22
# 4 d4 A1 NA
# 5 d5 A1 NA
# 6 d6 A1 NA
1
Amazing! Thank you so much! Appreciated. Could you add library(tidyr) to your answer for the gather function please?
– jjulip
2 days ago
I have updated my question as I have a large data frame with multiple blocks of data for the same animal and Days but different values and your solution does not work in this case. Any suggestions? This is the error message that I get: Error: Can't bind data because some arguments have the same name Callrlang::last_error()to see a backtrace
– jjulip
2 days ago
It is working for me even with the new data, try cleaning your workspace first then retry
– RLave
2 days ago
No. I've tried multiple times with multiple examples and I get the same error every time :(
– jjulip
2 days ago
which version ofdplyrandtidyrdo you have? SeepackageVersion("dplyr"). Try update to the last version by reinstalling both:devtools::install_github("hadley/dplyr")orinstall_packages("dplyr").
– RLave
2 days ago
add a comment |
up vote
5
down vote
With dplyr and tidyr:
library(dplyr)
library(tidyr)
df <- df %>%
gather("animal", "count", -Day)
head(df)
# Day animal count
# 1 d1 A1 14
# 2 d2 A1 24
# 3 d3 A1 22
# 4 d4 A1 NA
# 5 d5 A1 NA
# 6 d6 A1 NA
1
Amazing! Thank you so much! Appreciated. Could you add library(tidyr) to your answer for the gather function please?
– jjulip
2 days ago
I have updated my question as I have a large data frame with multiple blocks of data for the same animal and Days but different values and your solution does not work in this case. Any suggestions? This is the error message that I get: Error: Can't bind data because some arguments have the same name Callrlang::last_error()to see a backtrace
– jjulip
2 days ago
It is working for me even with the new data, try cleaning your workspace first then retry
– RLave
2 days ago
No. I've tried multiple times with multiple examples and I get the same error every time :(
– jjulip
2 days ago
which version ofdplyrandtidyrdo you have? SeepackageVersion("dplyr"). Try update to the last version by reinstalling both:devtools::install_github("hadley/dplyr")orinstall_packages("dplyr").
– RLave
2 days ago
add a comment |
up vote
5
down vote
up vote
5
down vote
With dplyr and tidyr:
library(dplyr)
library(tidyr)
df <- df %>%
gather("animal", "count", -Day)
head(df)
# Day animal count
# 1 d1 A1 14
# 2 d2 A1 24
# 3 d3 A1 22
# 4 d4 A1 NA
# 5 d5 A1 NA
# 6 d6 A1 NA
With dplyr and tidyr:
library(dplyr)
library(tidyr)
df <- df %>%
gather("animal", "count", -Day)
head(df)
# Day animal count
# 1 d1 A1 14
# 2 d2 A1 24
# 3 d3 A1 22
# 4 d4 A1 NA
# 5 d5 A1 NA
# 6 d6 A1 NA
edited 2 days ago
answered 2 days ago
RLave
2,5281820
2,5281820
1
Amazing! Thank you so much! Appreciated. Could you add library(tidyr) to your answer for the gather function please?
– jjulip
2 days ago
I have updated my question as I have a large data frame with multiple blocks of data for the same animal and Days but different values and your solution does not work in this case. Any suggestions? This is the error message that I get: Error: Can't bind data because some arguments have the same name Callrlang::last_error()to see a backtrace
– jjulip
2 days ago
It is working for me even with the new data, try cleaning your workspace first then retry
– RLave
2 days ago
No. I've tried multiple times with multiple examples and I get the same error every time :(
– jjulip
2 days ago
which version ofdplyrandtidyrdo you have? SeepackageVersion("dplyr"). Try update to the last version by reinstalling both:devtools::install_github("hadley/dplyr")orinstall_packages("dplyr").
– RLave
2 days ago
add a comment |
1
Amazing! Thank you so much! Appreciated. Could you add library(tidyr) to your answer for the gather function please?
– jjulip
2 days ago
I have updated my question as I have a large data frame with multiple blocks of data for the same animal and Days but different values and your solution does not work in this case. Any suggestions? This is the error message that I get: Error: Can't bind data because some arguments have the same name Callrlang::last_error()to see a backtrace
– jjulip
2 days ago
It is working for me even with the new data, try cleaning your workspace first then retry
– RLave
2 days ago
No. I've tried multiple times with multiple examples and I get the same error every time :(
– jjulip
2 days ago
which version ofdplyrandtidyrdo you have? SeepackageVersion("dplyr"). Try update to the last version by reinstalling both:devtools::install_github("hadley/dplyr")orinstall_packages("dplyr").
– RLave
2 days ago
1
1
Amazing! Thank you so much! Appreciated. Could you add library(tidyr) to your answer for the gather function please?
– jjulip
2 days ago
Amazing! Thank you so much! Appreciated. Could you add library(tidyr) to your answer for the gather function please?
– jjulip
2 days ago
I have updated my question as I have a large data frame with multiple blocks of data for the same animal and Days but different values and your solution does not work in this case. Any suggestions? This is the error message that I get: Error: Can't bind data because some arguments have the same name Call
rlang::last_error() to see a backtrace– jjulip
2 days ago
I have updated my question as I have a large data frame with multiple blocks of data for the same animal and Days but different values and your solution does not work in this case. Any suggestions? This is the error message that I get: Error: Can't bind data because some arguments have the same name Call
rlang::last_error() to see a backtrace– jjulip
2 days ago
It is working for me even with the new data, try cleaning your workspace first then retry
– RLave
2 days ago
It is working for me even with the new data, try cleaning your workspace first then retry
– RLave
2 days ago
No. I've tried multiple times with multiple examples and I get the same error every time :(
– jjulip
2 days ago
No. I've tried multiple times with multiple examples and I get the same error every time :(
– jjulip
2 days ago
which version of
dplyr and tidyr do you have? See packageVersion("dplyr"). Try update to the last version by reinstalling both: devtools::install_github("hadley/dplyr") or install_packages("dplyr").– RLave
2 days ago
which version of
dplyr and tidyr do you have? See packageVersion("dplyr"). Try update to the last version by reinstalling both: devtools::install_github("hadley/dplyr") or install_packages("dplyr").– RLave
2 days ago
add a comment |
up vote
2
down vote
It can be done with base R function melt:
melt(df)
OUTPUT :
Day variable value
1 d1 A1 14
2 d2 A1 24
3 d3 A1 22
4 d4 A1 NA
5 d5 A1 NA
6 d6 A1 NA
7 d7 A1 NA
8 d8 A1 NA
9 d9 A1 NA
10 d10 A1 NA
11 d1 A2 9
12 d2 A2 15
13 d3 A2 34
14 d4 A2 2
15 d5 A2 12
16 d6 A2 34
17 d7 A2 234
18 d8 A2 34
19 d9 A2 NA
20 d10 A2 NA
21 d1 A3 3
22 d2 A3 4
23 d3 A3 19
24 d4 A3 76
25 d5 A3 34
26 d6 A3 34
27 d7 A3 23
28 d8 A3 24
29 d9 A3 37
30 d10 A3 44
add a comment |
up vote
2
down vote
It can be done with base R function melt:
melt(df)
OUTPUT :
Day variable value
1 d1 A1 14
2 d2 A1 24
3 d3 A1 22
4 d4 A1 NA
5 d5 A1 NA
6 d6 A1 NA
7 d7 A1 NA
8 d8 A1 NA
9 d9 A1 NA
10 d10 A1 NA
11 d1 A2 9
12 d2 A2 15
13 d3 A2 34
14 d4 A2 2
15 d5 A2 12
16 d6 A2 34
17 d7 A2 234
18 d8 A2 34
19 d9 A2 NA
20 d10 A2 NA
21 d1 A3 3
22 d2 A3 4
23 d3 A3 19
24 d4 A3 76
25 d5 A3 34
26 d6 A3 34
27 d7 A3 23
28 d8 A3 24
29 d9 A3 37
30 d10 A3 44
add a comment |
up vote
2
down vote
up vote
2
down vote
It can be done with base R function melt:
melt(df)
OUTPUT :
Day variable value
1 d1 A1 14
2 d2 A1 24
3 d3 A1 22
4 d4 A1 NA
5 d5 A1 NA
6 d6 A1 NA
7 d7 A1 NA
8 d8 A1 NA
9 d9 A1 NA
10 d10 A1 NA
11 d1 A2 9
12 d2 A2 15
13 d3 A2 34
14 d4 A2 2
15 d5 A2 12
16 d6 A2 34
17 d7 A2 234
18 d8 A2 34
19 d9 A2 NA
20 d10 A2 NA
21 d1 A3 3
22 d2 A3 4
23 d3 A3 19
24 d4 A3 76
25 d5 A3 34
26 d6 A3 34
27 d7 A3 23
28 d8 A3 24
29 d9 A3 37
30 d10 A3 44
It can be done with base R function melt:
melt(df)
OUTPUT :
Day variable value
1 d1 A1 14
2 d2 A1 24
3 d3 A1 22
4 d4 A1 NA
5 d5 A1 NA
6 d6 A1 NA
7 d7 A1 NA
8 d8 A1 NA
9 d9 A1 NA
10 d10 A1 NA
11 d1 A2 9
12 d2 A2 15
13 d3 A2 34
14 d4 A2 2
15 d5 A2 12
16 d6 A2 34
17 d7 A2 234
18 d8 A2 34
19 d9 A2 NA
20 d10 A2 NA
21 d1 A3 3
22 d2 A3 4
23 d3 A3 19
24 d4 A3 76
25 d5 A3 34
26 d6 A3 34
27 d7 A3 23
28 d8 A3 24
29 d9 A3 37
30 d10 A3 44
answered 2 days ago
sai saran
55318
55318
add a comment |
add a comment |
up vote
2
down vote
You want to format your data frame from a wide format to a long format using the melt function of the reshape 2 package also answered here
library(reshape2)
df <- structure(list(Day = c("d1", "d2", "d3", "d4", "d5", "d6", "d7",
"d8", "d9", "d10"), A1 = c(14L, 24L, 22L, NA, NA, NA, NA, NA,
NA, NA), A2 = c(9L, 15L, 34L, 2L, 12L, 34L, 234L, 34L, NA, NA
), A3 = c(3L, 4L, 19L, 76L, 34L, 34L, 23L, 24L, 37L, 44L)), .Names = c("Day",
"A1", "A2", "A3"), class = "data.frame", row.names = c(NA, -10L
))
long_format <- melt(df)
colnames(long_format)[2:3] <- c("Animal","Count")
New contributor
seth-1 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
How does melt determine what the id variable is? Or how would I specify this? In my huge dataset it is having trouble doing this.
– jjulip
2 days ago
@jjulip you can try?melt.data.frameto get more information on the function. See e.g. under Arguments and id.vars.
– seth-1
2 days ago
add a comment |
up vote
2
down vote
You want to format your data frame from a wide format to a long format using the melt function of the reshape 2 package also answered here
library(reshape2)
df <- structure(list(Day = c("d1", "d2", "d3", "d4", "d5", "d6", "d7",
"d8", "d9", "d10"), A1 = c(14L, 24L, 22L, NA, NA, NA, NA, NA,
NA, NA), A2 = c(9L, 15L, 34L, 2L, 12L, 34L, 234L, 34L, NA, NA
), A3 = c(3L, 4L, 19L, 76L, 34L, 34L, 23L, 24L, 37L, 44L)), .Names = c("Day",
"A1", "A2", "A3"), class = "data.frame", row.names = c(NA, -10L
))
long_format <- melt(df)
colnames(long_format)[2:3] <- c("Animal","Count")
New contributor
seth-1 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
How does melt determine what the id variable is? Or how would I specify this? In my huge dataset it is having trouble doing this.
– jjulip
2 days ago
@jjulip you can try?melt.data.frameto get more information on the function. See e.g. under Arguments and id.vars.
– seth-1
2 days ago
add a comment |
up vote
2
down vote
up vote
2
down vote
You want to format your data frame from a wide format to a long format using the melt function of the reshape 2 package also answered here
library(reshape2)
df <- structure(list(Day = c("d1", "d2", "d3", "d4", "d5", "d6", "d7",
"d8", "d9", "d10"), A1 = c(14L, 24L, 22L, NA, NA, NA, NA, NA,
NA, NA), A2 = c(9L, 15L, 34L, 2L, 12L, 34L, 234L, 34L, NA, NA
), A3 = c(3L, 4L, 19L, 76L, 34L, 34L, 23L, 24L, 37L, 44L)), .Names = c("Day",
"A1", "A2", "A3"), class = "data.frame", row.names = c(NA, -10L
))
long_format <- melt(df)
colnames(long_format)[2:3] <- c("Animal","Count")
New contributor
seth-1 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
You want to format your data frame from a wide format to a long format using the melt function of the reshape 2 package also answered here
library(reshape2)
df <- structure(list(Day = c("d1", "d2", "d3", "d4", "d5", "d6", "d7",
"d8", "d9", "d10"), A1 = c(14L, 24L, 22L, NA, NA, NA, NA, NA,
NA, NA), A2 = c(9L, 15L, 34L, 2L, 12L, 34L, 234L, 34L, NA, NA
), A3 = c(3L, 4L, 19L, 76L, 34L, 34L, 23L, 24L, 37L, 44L)), .Names = c("Day",
"A1", "A2", "A3"), class = "data.frame", row.names = c(NA, -10L
))
long_format <- melt(df)
colnames(long_format)[2:3] <- c("Animal","Count")
New contributor
seth-1 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
seth-1 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered 2 days ago
seth-1
212
212
New contributor
seth-1 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
seth-1 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
seth-1 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
How does melt determine what the id variable is? Or how would I specify this? In my huge dataset it is having trouble doing this.
– jjulip
2 days ago
@jjulip you can try?melt.data.frameto get more information on the function. See e.g. under Arguments and id.vars.
– seth-1
2 days ago
add a comment |
How does melt determine what the id variable is? Or how would I specify this? In my huge dataset it is having trouble doing this.
– jjulip
2 days ago
@jjulip you can try?melt.data.frameto get more information on the function. See e.g. under Arguments and id.vars.
– seth-1
2 days ago
How does melt determine what the id variable is? Or how would I specify this? In my huge dataset it is having trouble doing this.
– jjulip
2 days ago
How does melt determine what the id variable is? Or how would I specify this? In my huge dataset it is having trouble doing this.
– jjulip
2 days ago
@jjulip you can try
?melt.data.frame to get more information on the function. See e.g. under Arguments and id.vars.– seth-1
2 days ago
@jjulip you can try
?melt.data.frame to get more information on the function. See e.g. under Arguments and id.vars.– seth-1
2 days ago
add a comment |