简体   繁体   English

基于 R 中的其他列创建列序列

[英]Create column sequence based on other columns in R

I have data such as this.我有这样的数据。

        data.sample <- read_table2('score_label treatment   score   data1   data2   data3
A   treatment   1   1   t   yt
A   treatment   2   1   t   yt
A   treatment   3   5   f   yt
B   treatment   1   5   f   yt
B   treatment   2   5   f   yt
B   treatment   3   5.5 g   yt
B   treatment   4   6.8 t   yt
C   treatment   1   9.4 t   yt
C   treatment   2   10.7    f   yt
C   treatment   3   12  j   yt
C   treatment   4   13.3    t   yt
C   control 1   14.6    t   yt
C   control 3   18.5    k   yt
C   control 4   19.8    t   yt')

I would like to create df such as this.我想创建这样的 df。 Where every score label-treatment group, has a score running from 1-4 and where 0 is populated into the cells where this score was not present previously.每个分数标签处理组都有一个从 1 到 4 的分数,其中 0 填充到以前不存在该分数的单元格中。

output<- read_table2('score_label   treatment   score   data1   data2   data3
A   treatment   1   1   t   yt
A   treatment   2   1   t   yt
A   treatment   3   5   f   yt
A   treatment   4   0   0   0
B   treatment   1   5   f   yt
B   treatment   2   5   f   yt
B   treatment   3   5.5 g   yt
B   treatment   4   6.8 t   yt
C   treatment   1   9.4 t   yt
C   treatment   2   10.7    f   yt
C   treatment   3   12  j   yt
C   treatment   4   13.3    t   yt
C   control 1   14.6    t   yt
C   control 2   0   0   0
C   control 3   18.5    k   yt
C   control 4   19.8    t   yt') 

I thought of doing this to create a new score column, but it's not working how I hoped it would.我想这样做是为了创建一个新的分数列,但它并没有像我希望的那样工作。 Any suggestions appreciated!!任何建议表示赞赏!

data.sample %>%
group_by(score_lable, treatment) %>%
mutate(new_score=seq(4)) 

We can use complete with fill我们可以使用complete with fill

library(dplyr)
library(tidyr)
data.sample %>% 
    group_by(score_label, treatment) %>% 
    complete(score = unique(data.sample$score),
          fill = list(data1 = 0, data2 = 0, data3 = '0'))

If there are many columns to fill , it can be constructed as a list如果要fill列数很多,则可以将其构造为list

nm1 <- names(data.sample)[startsWith(names(data.sample), 'data')]
fillcols <- setNames(rep(list(0), length(nm1)), nm1)
data.sample %>% 
  group_by(score_label, treatment) %>% 
  complete(score = unique(data.sample$score), fill = fillcols)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM