Reputation: 33
I have a table looking like this (>200 readings, duplicate samples):
**A B C**
Site1 1 2 3
Site1 4 5 6
Site2 7 8 9
Site2 10 11 12
What code can be used to get output of:
**X**
1
4
2
5
3
6
7
10
8
11
9
12
Upvotes: 2
Views: 45
Reputation: 887048
Assuming the dataset is a matrix
(as data.frame
cannot have duplicate row names), split
by the rownames and unlist
the list
data.frame(X=unlist(split(m1, row.names(m1)), use.names = FALSE))
# X
#1 1
#2 4
#3 2
#4 5
#5 3
#6 6
#7 7
#8 10
#9 8
#10 11
#11 9
#12 12
m1 <- structure(c(1L, 4L, 7L, 10L, 2L, 5L, 8L, 11L, 3L, 6L, 9L, 12L
), .Dim = c(4L, 3L), .Dimnames = list(c("Site1", "Site1", "Site2",
"Site2"), c("A", "B", "C")))
Upvotes: 1
Reputation: 39154
Assuming that your dataset is a data frame, you can consider using dplyr
and tidyr
. Notice that data frame cannot have duplicated row names, so I assume that there is a column called Site
documented if the record is Site1
or Site2
.
library(dplyr)
library(tidyr)
# Create example data frame
dt <- read.table(text = "Site A B C
Site1 1 2 3
Site1 4 5 6
Site2 7 8 9
Site2 10 11 12",
header = TRUE, stringsAsFactors = FALSE)
dt
# Site A B C
# 1 Site1 1 2 3
# 2 Site1 4 5 6
# 3 Site2 7 8 9
# 4 Site2 10 11 12
dt2 <- dt %>%
gather(Col, X, -Site) %>%
arrange(Site) %>%
select(X)
dt2
# X
# 1 1
# 2 4
# 3 2
# 4 5
# 5 3
# 6 6
# 7 7
# 8 10
# 9 8
# 10 11
# 11 9
# 12 12
Upvotes: 2
Reputation: 1560
If what you have (let's call it data
) is of class matrix
then you can use the following function:
X <- as.vector(t(data))
If it is a data.frame
transform it into a matrix, because I see that all your entries are numbers.
Upvotes: 0