R 有效地绑定不同属性的行

R 有效地绑定不同属性的行,r,tidyverse,tidytable,R,Tidyverse,Tidytable,我想装订成几行。但是,data.frames中很少有列具有不同的属性。像df1$caseid和df1$v001与df2$caseid和df2$v001具有不同的属性。不知道如何才能绑定这些data.frames library(tidyverse) library(tidytable) #> #> Attaching package: 'tidytable' #> The following object is masked from 'package:stats': #&g

我想装订成几行。但是,data.frames中很少有列具有不同的属性。像
df1$caseid
df1$v001
df2$caseid
df2$v001
具有不同的属性。不知道如何才能绑定这些data.frames

library(tidyverse)
library(tidytable)
#> 
#> Attaching package: 'tidytable'
#> The following object is masked from 'package:stats':
#> 
#>     dt

df1 <- 
  structure(list(caseid = structure(c("   11 1  1 1  2", "   11 1  1 1  2", 
"   11 1  1 1  2", "   11 1  1 1  2", "   11 1  1 1  2", "   11 1  1 2  2"
), label = "case identification", class = c("labelled", "character"
), format = "%15s"), bidx = structure(c(1L, 2L, 3L, 4L, 5L, 1L
), label = "birth column number", class = c("labelled", "integer"
), format = "%8.0g"), v000 = structure(c("PK2", "PK2", "PK2", 
"PK2", "PK2", "PK2"), label = "country code and phase", class = c("labelled", 
"character"), format = "%3s"), v001 = structure(c(1101001L, 1101001L, 
1101001L, 1101001L, 1101001L, 1101001L), label = "cluster number", class = c("labelled", 
"integer"), format = "%12.0g"), v002 = structure(c(1L, 1L, 1L, 
1L, 1L, 2L), label = "household number", class = c("labelled", 
"integer"), format = "%8.0g")), row.names = c(NA, -6L), class = "data.frame")

df2 <- 
  structure(list(caseid = structure(c(1L, 1L, 1L, 1L, 1L, 2L), .Label = c("       1   1  2", 
"       1   4  1"), class = "factor"), bidx = structure(c(1L, 
2L, 3L, 4L, 5L, 1L), label = c(BIDX = "Birth column number"), class = c("labelled", 
"numeric")), v000 = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "PK7", class = "factor"), 
    v001 = structure(c(1L, 1L, 1L, 1L, 1L, 1L), label = c(V001 = "Cluster number"), class = c("labelled", 
    "numeric")), v002 = structure(c(1L, 1L, 1L, 1L, 1L, 4L), label = c(V002 = "Household number"), class = c("labelled", 
    "numeric"))), row.names = c(NA, -6L), class = "data.frame")

rbind(df1, df2)
#>             caseid bidx v000    v001 v002
#> 1     11 1  1 1  2    1  PK2 1101001    1
#> 2     11 1  1 1  2    2  PK2 1101001    1
#> 3     11 1  1 1  2    3  PK2 1101001    1
#> 4     11 1  1 1  2    4  PK2 1101001    1
#> 5     11 1  1 1  2    5  PK2 1101001    1
#> 6     11 1  1 2  2    1  PK2 1101001    2
#> 7         1   1  2    1  PK7       1    1
#> 8         1   1  2    2  PK7       1    1
#> 9         1   1  2    3  PK7       1    1
#> 10        1   1  2    4  PK7       1    1
#> 11        1   1  2    5  PK7       1    1
#> 12        1   4  1    1  PK7       1    4

bind_rows(df1, df2)
#> Error: Can't combine `..1$caseid` <labelled> and `..2$caseid` <factor<da793>>.

bind_rows.(df1, df2)
#> Error in rbindlist(dots, idcol = .id, use.names = .use_names, fill = .fill): Class attribute on column 2 of item 2 does not match with column 2 of item 1.
库(tidyverse)
图书馆(整洁)
#> 
#>附加包:“整洁表”
#>以下对象已从“package:stats”屏蔽:
#> 
#>dt
df1 11 1 2 1 PK2 1101001 1
#>2 11 11 12 PK2 1101001 1
#>3 11 11 1 2 3 PK2 1101001 1
#>4 11 11 1 2 4 PK2 1101001 1
#>5 11 11 1 2 5 PK2 1101001 1
#>6 11 1 2 1 PK2 1101001 2
#>7121PK711
#>81212PK711
#>9 1 2 3 PK7 1 1 1
#>10124PK711
#>11 1 2 5 PK7 1 1
#>12141PK714
绑定行(df1、df2)
#>错误:无法组合“…1$caseid”和“…2$caseid”。
绑定行(df1、df2)
#>rbindlist中出错(点,idcol=.id,use.names=.use_names,fill=.fill):项目2第2列上的Class属性与项目1第2列不匹配。

听起来,无论发生什么情况,您都需要修复列类以进行匹配。如果数值列始终为整数,请将
df2
中的类更改为
integer

i <- sapply(df2, is.numeric)
df2[i] <- lapply(df2[i], as.integer)

如果需要将这些列作为因子,请在绑定行后重构它们。

rbind
对您有用,对吗?问题是什么?@RonakShah yes
rbind
适用于这些小型
数据。帧
但不适用于大型数据集。另外,
tidyverse
中的
bind_行
bind_行。
from
tidytable
即使对于这些小的
数据帧
也不起作用。因此,寻找一种有效的方法。
rbind
会发生什么?它是否会出错(是什么?)或速度非常慢<代码>绑定行在类不同时不起作用。它总是会给出一个错误。@Ronaksah:对于我的实际数据集
rbind
,在rbindlist(l,use.names,fill,idcol)中抛出以下错误消息
错误:第2项第2列的Class属性与第1项第2列的Class属性不匹配
。请尝试使用
base::rbind
i <- sapply(df2, is.factor)
df2[i] <- lapply(df2[i], as.character)