首页 > 解决方案 > 根据 R 中的列值复制行

问题描述

好的,我有以下数据集:

df = read.table(sep=",",  
    header=T,   
    text="combination,priority,boolean,value      
        0,1,True,1.4  #should be copied  
        0,2,True,2.0  #should be copied  
        1,1,True,3.2  
        1,2,True,54.2  
        2,1,False,12.1  
        2,2,False,44.1  
        ")

有没有一种简洁的方法:

  1. 使用组合复制所有行== 0
  2. 对于组合中的每个值!=0
  3. AND 复制的行应该采用组合的值!=0

预期输出:

df_new = read.table(sep=",",  
    header=T,   
    text="combination,priority,boolean,value      
        1,1,True,1.4  #copied row -> now comb==1  
        1,2,True,2.0  #copied row -> now comb==1  
        1,1,True,3.2  
        1,2,True,54.2  
        2,1,True,1.4  #copied row -> now comb==2  
        2,2,True,2.0  #copied row -> now comb==2  
        2,1,False,12.1  
        2,2,False,44.1  
        ")

标签: r

解决方案


使用 data.table,它有点复杂但可行:

library(data.table)
df <- setDT(df)
df_zero <- df[combination == 0]
# now combine the rows of df where combination !=0 with copies of the rows where
# combination does equal 0, taking on the non-zero combination values
df_zero <- df_zero[rep(seq_len(nrow(df_zero)), each = length(unique(df[combination!=0]$combination))), ]
df_zero[, combination := rep(unique(df[combination!=0]$combination), nrow(df[combination==0]))]
df <- rbind(df[combination!=0], df_zero)
df

     combination priority boundry       mean
  1:           1        3       f 3.57246241
  2:           1        3       t 0.22327863
  3:           1        5       t 0.05760450
  4:           2        3       f 3.47917124
  5:           2        3       t 0.26262743
 ---                                        
102:          14        5       t 0.05368306
103:          15        5       t 0.05368306
104:          16        5       t 0.05368306
105:          17        5       t 0.05368306
106:          18        5       t 0.05368306

推荐阅读