Reputation: 461
I have a dataset and a subset looks as:
Sample1 Sample2
Weight1 20.5 34.8
Weight2 54.5 23.7
I want to create a variable representing all the values like 20.5, 34.8, 54.5, 23.7. I have a bigger dataset and cannot put all the values manually so I want to make a variable representing the values. Thank you!
Upvotes: 0
Views: 46
Reputation: 869
From what you described in your questiom, it looks that you are looking for a pivot operation. And from the output that you printed in your question, it looks like your data is currently being interpreted in R, as a matrix. So the first thing I would do, is to transform the data in a data.frame, with as.data.frame()
, them, I would paste the row.names()
, to a column like this:
data <- as.data.frame(input)
data$Weight <- row.names(data)
After that, I would use the pivot_longer()
function, from tidyr
package, to get the result (that I think) you want. Like this:
library(tidyr)
data <- pivot_longer(
data,
cols = -Weight,
names_to = "Sample",
values_to = "Value"
)
# A tibble: 4 x 3
Weight Sample Value
<chr> <chr> <dbl>
1 Weight1 Sample1 20.5
2 Weight1 Sample2 34.8
3 Weight2 Sample1 54.5
4 Weight2 Sample2 23.7
Upvotes: 1