我有一张桌子:
sample tomato zucchini broccoli
a x x
b x
c x x我想要这样做:
a tomato
a broccoli
b zucchini
c tomato
c broccoli你对R有什么建议吗?提前感谢
发布于 2019-12-18 12:43:57
我将使用来自tidyr包的tidyr(或者可能更简单的tidyverse)。
加载一些库:
library(tidyverse)你的数据:
my_df <- tribble(
~sample, ~tomato, ~zucchini, ~broccoli,
"a", "x", NA, "x",
"b", NA, "x", NA,
"c", "x", NA, "x",
)代码:(基于注释@Ronak Shah更新)
my_df <- my_df %>%
# make table long format
pivot_longer(cols = -sample,
names_to = "vegy",
values_to = "value",
values_drop_na = TRUE) %>%
# get rid of value column
select(-value)
my_df
# A tibble: 5 x 2
sample vegy
<chr> <chr>
1 a tomato
2 a broccoli
3 b zucchini
4 c tomato
5 c broccoli发布于 2019-12-18 13:15:02
下面是使用base R的解决方案,其中apply()和rep()是关键,即,
r <- apply(df, 1, function(v) names(v[-1])[which(v[-1] =="x")])
dfout <- data.frame(sample = rep(df$sample,lengths(r)),veg = unlist(r))这样的话
> dfout
sample veg
1 a tomato
2 a broccoli
3 b zucchini
4 c tomato
5 c broccoli数据
df <- structure(list(sample = structure(1:3, .Label = c("a", "b", "c"
), class = "factor"), tomato = structure(c(1L, NA, 1L), .Label = "x", class = "factor"),
zucchini = structure(c(NA, 1L, NA), .Label = "x", class = "factor"),
broccoli = structure(c(1L, NA, 1L), .Label = "x", class = "factor")), class = "data.frame", row.names = c(NA,
-3L))
> df
sample tomato zucchini broccoli
1 a x <NA> x
2 b <NA> x <NA>
3 c x <NA> xhttps://stackoverflow.com/questions/59392174
复制相似问题