简体   繁体   English

根据行中特定值的存在替换行中的值

[英]Replacing Values in Rows based on Existence of Specific Value in Row

Here is the data set I am working with: 这是我正在使用的数据集:

ID    x1    x2    x3    x4        x5        
1     0     0     1     .25       .25
2     0     .5    .5    1         Complete
3     0     .25   .25   .5        .75 
4     0     .5    1     Complete  Complete

When you see the value of 1 occur in a row, all remaining values in that row should show as "Complete." 当看到值1连续出现时,该行中的所有剩余值应显示为“完成”。 I'm trying to fix an issue in this data where you can see that at least one row does not follow that rule (Row 1). 我正在尝试解决此数据中的问题,您可以看到至少有一行未遵循该规则(行1)。 I want to find a way, either by for loop, mutate in tidy, etc. to cycle through my data set and read each row until it finds the value of 1 in that row if applicable. 我想找到一种方法,可以通过for循环,整洁的变异等方式循环遍历我的数据集并读取每一行,直到找到该行中的1(如果适用)的值。 If it finds that value of 1, have it replace all remaining values with "Complete" in that row as that should be the default anyway. 如果发现该值为1,则将其所有剩余值替换为该行中的“完成”,因为无论如何这都是默认值。

This should be what the data looks like: 数据应如下所示:

ID    x1    x2    x3    x4        x5        
1     0     0     1     Complete  Complete
2     0     .5    .5    1         Complete
3     0     .25   .25   .5        .75 
4     0     .5    1     Complete  Complete

What I have now is: 我现在所拥有的是:

for(i in 1:nrow(data)){ position <- which(data[i, ]==1)
                        data[,position+1] <- "Complete" }

Check this solution: 检查此解决方案:

library(dplyr)
library(tidyr)
df %>%
  gather(key, val, x1:x5) %>%
  group_by(ID) %>%
  mutate(
    check = val == 1,
    check = cumsum(check),
    val = if_else(
      val < 1 & check == 1 | val == 1 & check > 1,
      'Complete',
      as.character(val)
    )
  ) %>%
  select(-check) %>%
  spread(key, val)

A different tidyverse approach could be: tidyverse方法可以是:

df %>%
 gather(var, val, -ID) %>%
 group_by(ID) %>%
 mutate(val = ifelse(row_number() > row_number(val == "1"), "Complete", val)) %>%
 spread(var, val)

     ID x1    x2    x3    x4       x5      
  <int> <chr> <chr> <chr> <chr>    <chr>   
1     1 0     0     1     Complete Complete
2     2 0     0.5   0.5   1        Complete
3     3 0     0.25  0.25  .5       .75     
4     4 0     0.5   1     Complete Complete

In the first step, it transforms the data from wide to long format. 第一步,它将数据从宽格式转换为长格式。 Secondly, it assigns "Complete" if the row number (per group) is bigger than the row number of the row with value 1. Finally, it transforms the data back to the original format. 其次,如果行号(每组)大于具有值1的行的行号,则分配“完成”。最后,它将数据转换回原始格式。

An "old school" solution: 一个“老派”解决方案:

ncol <- dim(data)[2]
for (i in 1:nrow(data)){
  position <- which(data[i, 2:ncol] == 1) + 1 
  if (length(position) != 0) {
    data[i, (position + 1):ncol] <- "Complete"
  }
}
data

  ID x1   x2   x3       x4       x5
1  1  0 0.00 1.00 Complete Complete
2  2  0 0.50 0.50        1 Complete
3  3  0 0.25 0.25       .5      .75
4  4  0 0.50 1.00 Complete Complete

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

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