tidyr :: spread导致产生多个行

时间:2018-08-06 18:44:43

标签: r tidyr spread

我有一个与以下问题类似的问题,但以下链接中介绍的解决方案不适用于我: tidyr spread does not aggregate data

我有以下结构的df:

    UndesiredIndex  DesiredIndex    DesiredRows Result
1   x1A x1  A   50,32
2   x1B x2  B   7,34
3   x2A x1  A   50,33
4   x2B x2  B   7,35

使用以下代码:

  dftest <- bd_teste %>%
  select(-UndesiredIndex) %>%
  spread(DesiredIndex, Result)

我希望得到以下结果:

DesiredIndex    A   B
A   50,32   50,33
B   7,34    7,35

尽管如此,我仍然得到以下结果:

    DesiredIndex    x1  x2
1   A   50.32   NA
2   B   7.34    NA
3   A   NA  50.33
4   B   NA  7.35

PS:有时我用select(-UndesiredIndex)强制取消UndesiredIndex列,但我不断收到以下消息:

添加缺少的分组变量:UndesiredIndex

堆叠这些行可能很容易,但是我是R的新手,并且一直在努力解决这个问题,但没有成功。 预先感谢!

2 个答案:

答案 0 :(得分:1)

我们通过'DesiredIndex'分组,创建一个序列列,然后执行spread

library(tidy verse)
df1 %>% 
    select(-UndesiredIndex) %>% 
    group_by(DesiredIndex) %>% 
    mutate(new = LETTERS[row_number()]) %>% 
    ungroup %>%
    select(-DesiredIndex) %>% 
    spread(new, Result)
# A tibble: 2 x 3
#  DesiredRows A     B    
#  <chr>       <chr> <chr>
#1 A           50,32 50,33
#2 B           7,34  7,35 

数据

df1 <- structure(list(UndesiredIndex = c("x1A", "x1B", "x2A", "x2B"), 
DesiredIndex = c("x1", "x2", "x1", "x2"), DesiredRows = c("A", 
"B", "A", "B"), Result = c("50,32", "7,34", "50,33", "7,35"
)), class = "data.frame", row.names = c("1", "2", "3", "4"
))

答案 1 :(得分:1)

更狭窄,但理论上更容易绕过。

# Data (thanks to Akrun!)
df1 <- structure(list(UndesiredIndex = c("x1A", "x1B", "x2A", "x2B"), 
DesiredIndex = c("x1", "x2", "x1", "x2"), DesiredRows = c("A", 
"B", "A", "B"), Result = c("50,32", "7,34", "50,33", "7,35"
)), class = "data.frame", row.names = c("1", "2", "3", "4"
))

这是连接行的一种很棒的技术。

df1 %>% 
  group_by(DesiredRows) %>% 
  summarise(Result = paste(Result, collapse = "|")) %>% #<Concatenate rows
  separate(Result, into = c("A", "B"), sep = "\\|")     #<Separate by '|'

#> # A tibble: 2 x 3
#>   DesiredRows A     B    
#>   <chr>       <chr> <chr>
#> 1 A           50,32 50,33
#> 2 B           7,34  7,35

reprex package(v0.2.0)于2018-08-06创建。