绑定用法返回意外结果

问题描述 投票:0回答:1

我有一个小标题abc和一个小标题定义。

abc

# A tibble: 4 x 4
# Groups:   Category, Measure [1]
  Category Measure     Date          Value
  <chr>    <chr>       <date>        <dbl>
1 Pickles  Gross Sales 2016-01-03 6518758.
2 Pickles  Gross Sales 2016-01-10 5640691.
3 Pickles  Gross Sales 2016-01-17 5450121.
4 Pickles  Gross Sales 2016-01-24 5726041.

def

# A tibble: 4 x 2
        all       pb
      <dbl>    <dbl>
1 25992010. 4836410.
2 25769111. 4692100.
3 25612548. 4555298.
4 23869990. 4180362.

我想将abc和def绑定在一起以获得6列的标题。

但是当我执行cbind(abc,def)时,我得到了以下内容。我不知道为什么。有人可以帮忙吗?

       abc         def      
Category Character,4 Numeric,4
Measure  Character,4 Numeric,4
Date     Numeric,4   Numeric,4
Value    Numeric,4   Numeric,4
r cbind
1个回答
0
投票

在结构中,其中一个与组属性分组在一起,因此,如果我们将ungroup或转换为data.frameas.data.frame-删除属性,它应该起作用]

library(dplyr)
abc %>%
   ungroup %>%
   cbind(def)

可以用mtcars复制

cbind(as_tibble(head(mtcars[1:4], 3)) %>% 
       group_by(cyl), 
       as_tibble(head(mtcars[5:7]), 3))
#     [,1]      [,2]     
#mpg  Numeric,3 Numeric,6
#cyl  Numeric,3 Numeric,6
#disp Numeric,3 Numeric,6
#hp   Numeric,3 Numeric,6

没有组属性

© www.soinside.com 2019 - 2024. All rights reserved.