'How to stack values columns with different datatype into a single column [duplicate]
I have a data set like this:
dt <- tibble(
TRIAL = c("A", "A", "A", "B", "B", "B", "C", "C", "C"),
GYF = c(1, 2, 1, 1, 6, 3, 2, 3, 1),
AD = c("A","B","C","D","E","F","G","H","I")
)
# # A tibble: 9 x 3
# TRIAL GYF AD
# <chr> <dbl> <dbl>
# 1 A 1 A
# 2 A 2 B
# 3 A 1 C
# 4 B 1 D
# 5 B 6 E
# 6 B 3 F
# 7 C 2 G
# 8 C 3 H
# 9 C 1 I
This is what I want to achieve: I want all values of GYF and FY column to be stacked and having the Header and the Trial Value in the first column.
# # A tibble: 18 x 3
# TRIAL Trait Traitvalue
# <chr> <chr> <dbl>
# 1 A GYF 1
# 2 A GYF 2
# 3 A GYF 3
# 4 A AD A
# 5 A AD B
# 6 A AD C
# 7 B GYF 1
# 8 B GYF 6
# 9 B GYF 3
# 10 B AD D
# 11 B AD E
# 12 B AD F
# 13 C GYF 2
# 14 C GYF 3
# 15 C GYF 1
# 16 C AD G
# 17 C AD H
# 18 C AD I
When I run this code I get not the expected output
dt2 %>% pivot_longer(., c("GYF","AD")) %>%
arrange(., TRIAL, name) %>%
rename(Traitvalue=value,
Trait=name)
Gave me the Error( Error: Can't combine GYF and FY .)
How can I possibly bypass the datatype issue to achieve my desired result.
Thank you
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
