Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为R中的数据框中的列填充缺少的字段_R_Dataframe - Fatal编程技术网

为R中的数据框中的列填充缺少的字段

为R中的数据框中的列填充缺少的字段,r,dataframe,R,Dataframe,我有以下专栏: casenum box type number of balls in the box 1 A 10 1 B 20 2 B 1 2 C 2 2 D 12 3 A 10 3

我有以下专栏:

casenum  box type  number of balls in the box
  1         A               10
  1         B               20
  2         B               1
  2         C               2
  2         D               12
  3         A               10
  3         B               20
  3         C               1
  3         D               2
  .         .               .
  .         .               .
  .         .               .
基本上有4种箱子类型(A、B、C、D),对于每个箱子编号,如果箱子中没有球,它就不会出现。但是,我希望每个框类型都像这样显示

casenum  box type  number of balls in the box
  1         A               10
  1         B               20
  1         C               0
  1         D               0
  1         A               0
  2         B               1
  2         C               2
  2         D               12
  3         A               10
  3         B               20
  3         C               1
  3         D               2
  .         .               .
  .         .               .
  .         .               .
有没有一个简单的方法

或者我可以用一种格式

casenum    ballnum in A     ballnum in B     ballnum in C     ballnum in D 
  1            10                20              0                  0
  2            0                  1              2                 12
  3            10                20              1                  2
  .            .                  .              .                  .
  .            .                  .              .                  .

我使用while loop来实现这一点,但我想知道是否有一种方法(使用一些我不知道的库)可以不使用循环来实现这一点。

我将创建一个新的data.frame,其中包含box和casenum的所有可能组合,然后添加球的数量:

df<-read.table(text="casenum  box  number
1         A               10
1         B               20
2         B               1
2         C               2
2         D               12
3         A               10
3         B               20
3         C               1
3         D               2", header=T)

dftot <- data.frame(casenum=rep(1:3, each=4), box=c("A","B","C","D"), stringsAsFactors = F) #create new df with all combinations of casenum and box
dftot$number <- df$number[match(paste(dftot$casenum,dftot$box),paste(df$casenum, df$box))] #add numbers from your original data.frame
dftot$number[is.na(dftot$number)] <- 0 #change all NA values to 0

df是基本R中
xtabs
的工作,其中
df
是您的数据帧:

xtabs(number~., df)

#       box
#casenum  A  B  C  D
#      1 10 20  0  0
#      2  0  1  2 12
#      3 10 20  1  2

我们可以使用
acast
from
restrape2

library(reshape2)
acast(df, casenum~box, fill=0)
#   A  B C  D
#1 10 20 0  0
#2  0  1 2 12
#3 10 20 1  2