如果满足条件,替换特定列上的值。

huangapple go评论77阅读模式
英文:

Replace value on specific column if condition is met

问题

我有一个名为wide df的数据框,其中columns代表了许多给定years中每个month的颜色变化:

df <- data.frame(id = as.integer(c(123,124,125,126)),
                 change = as.integer(c(0,1,0,1)),
                 change_date = as.character(c(NA, "May.2010", NA, "Sep.2010")),
                 `Jan.2010` = as.character(c("green", "black", "pink", "grey")),
                 `Feb.2010` = as.character(c("green", "black", "pink", "grey")),
                 `Mar.2010` = as.character(c("green", "red", "pink", "grey")),
                 `Apr.2010` = as.character(c("green", "red", "pink", "grey")),
                 `May.2010` = as.character(c("green", "red", "pink", "grey")),
                 `Jun.2010` = as.character(c("green", NA, "pink", "grey")),
                 `Jul.2010` = as.character(c("green", NA, "pink", "grey")),
                 `Ago.2010` = as.character(c("red", NA, "pink", "grey")),
                 `Sep.2010` = as.character(c("red", NA, "pink", "grey")),
                 `Oct.2010` = as.character(c("red", NA, "pink", NA)),
                 `Nov.2010` = as.character(c("red", NA, "pink", NA)),
                 `Dez.2010` = as.character(c("red", NA, "grey", NA))
)
df

如果发生变化(change == 1),我希望发生变化的相应列month.year接收值"CHANGE":

  id change change_date Jan.2010 Feb.2010 Mar.2010 Apr.2010 May.2010 Jun.2010 Jul.2010 Ago.2010 Sep.2010 Oct.2010 Nov.2010 Dez.2010
1 123      0        <NA>    green    green    green    green    green    green    green      red      red      red      red      red
2 124      1    May.2010    black    black      red      red   CHANGE     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>
3 125      0        <NA>     pink     pink     pink     pink     pink     pink     pink     pink     pink     pink     pink     grey
4 126      1    Sep.2010     grey     grey     grey     grey     grey     grey     grey     grey   CHANGE     <NA>     <NA>     <NA>

我尝试过的解决方案都以不同的方式失败了。感谢您的帮助。

英文:

I have a wide df with columns representing the months of many given years and the changes of colour in each month:

df <- data.frame(id = as.integer(c(123,124,125,126)),
                 change = as.integer(c(0,1,0,1)),
                 change_date = as.character(c(NA, "May.2010", NA, "Sep.2010")),
                 `Jan.2010` = as.character(c("green", "black", "pink", "grey")),
                 `Feb.2010` = as.character(c("green", "black", "pink", "grey")),
                 `Mar.2010` = as.character(c("green", "red", "pink", "grey")),
                 `Apr.2010` = as.character(c("green", "red", "pink", "grey")),
                 `May.2010` = as.character(c("green", "red", "pink", "grey")),
                 `Jun.2010` = as.character(c("green", NA, "pink", "grey")),
                 `Jul.2010` = as.character(c("green", NA, "pink", "grey")),
                 `Ago.2010` = as.character(c("red", NA, "pink", "grey")),
                 `Sep.2010` = as.character(c("red", NA, "pink", "grey")),
                 `Oct.2010` = as.character(c("red", NA, "pink", NA)),
                 `Nov.2010` = as.character(c("red", NA, "pink", NA)),
                 `Dez.2010` = as.character(c("red", NA, "grey", NA))
)
df  

  id change change_date Jan.2010 Feb.2010 Mar.2010 Apr.2010 May.2010 Jun.2010 Jul.2010 Ago.2010 Sep.2010 Oct.2010 Nov.2010 Dez.2010
1 123      0        <NA>    green    green    green    green    green    green    green      red      red      red      red      red
2 124      1    May.2010    black    black      red      red      red     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>
3 125      0        <NA>     pink     pink     pink     pink     pink     pink     pink     pink     pink     pink     pink     grey
4 126      1    Sep.2010     grey     grey     grey     grey     grey     grey     grey     grey     grey     <NA>     <NA>     <NA>

If a change occurs (change == 1), I want the correspondent column month.year in which the change occurs to receive the value "CHANGE":

  id change change_date Jan.2010 Feb.2010 Mar.2010 Apr.2010 May.2010 Jun.2010 Jul.2010 Ago.2010 Sep.2010 Oct.2010 Nov.2010 Dez.2010
1 123      0        <NA>    green    green    green    green    green    green    green      red      red      red      red      red
2 124      1    May.2010    black    black      red      red   CHANGE     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>
3 125      0        <NA>     pink     pink     pink     pink     pink     pink     pink     pink     pink     pink     pink     grey
4 126      1    Sep.2010     grey     grey     grey     grey     grey     grey     grey     grey   CHANGE     <NA>     <NA>     <NA>

The solutions I've tried failed in different ways. I appreciate your help on this.

答案1

得分: 3

在基本的R语言中,您可以这样做:

# 找出需要更改的行和对应的列
with(df,
  data.frame(
    index = which(change == 1),
    column = change_date[change == 1]
  )
) -> to_change

# 使用该对象在循环中进行所有更改
for(i in seq_along(to_change$index)){
  df[to_change$index[i], to_change$column[i]] = "CHANGE"
}

请注意,这是代码示例,不是完整的句子或段落。

英文:

In base R, you can do it like this:

# Work out which rows need changed, and which corresponding column
with(df,
  data.frame(
    index = which(change == 1),
    column = change_date[change == 1]
  )
) -> to_change

# Use that object to make all the changes in a loop
for(i in seq_along(to_change$index)){
  df[to_change$index[i], to_change$column[i]] = "CHANGE"
}

答案2

得分: 3

这是基于R语言的向量化方法 -

# 行号
row <- which(df$change == 1)
# 列号
col <- match(df$change_date[row], names(df))
# 创建一个包含行/列索引以更改值的矩阵
df[cbind(row, col)] <- "CHANGE"
df

#   id change change_date Jan.2010 Feb.2010 Mar.2010 Apr.2010 May.2010
#1 123      0        <NA>    green    green    green    green    green
#2 124      1    May.2010    black    black      red      red   CHANGE
#3 125      0        <NA>     pink     pink     pink     pink     pink
#4 126      1    Sep.2010     grey     grey     grey     grey     grey

#  Jun.2010 Jul.2010 Ago.2010 Sep.2010 Oct.2010 Nov.2010 Dez.2010
#1    green    green      red      red      red      red      red
#2     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>     <NA>
#3     pink     pink     pink     pink     pink     pink     grey
#4     grey     grey     grey   CHANGE     <NA>     <NA>     <NA>
英文:

Here is vectorised base R way -

# Row numbers
row &lt;- which(df$change == 1)
# Column numbers
col &lt;- match(df$change_date[row], names(df))
# Create a matrix of row/column index to change the values
df[cbind(row, col)] &lt;- &quot;CHANGE&quot;
df

#   id change change_date Jan.2010 Feb.2010 Mar.2010 Apr.2010 May.2010
#1 123      0        &lt;NA&gt;    green    green    green    green    green
#2 124      1    May.2010    black    black      red      red   CHANGE
#3 125      0        &lt;NA&gt;     pink     pink     pink     pink     pink
#4 126      1    Sep.2010     grey     grey     grey     grey     grey

#  Jun.2010 Jul.2010 Ago.2010 Sep.2010 Oct.2010 Nov.2010 Dez.2010
#1    green    green      red      red      red      red      red
#2     &lt;NA&gt;     &lt;NA&gt;     &lt;NA&gt;     &lt;NA&gt;     &lt;NA&gt;     &lt;NA&gt;     &lt;NA&gt;
#3     pink     pink     pink     pink     pink     pink     grey
#4     grey     grey     grey   CHANGE     &lt;NA&gt;     &lt;NA&gt;     &lt;NA&gt;

huangapple
  • 本文由 发表于 2023年7月6日 18:11:40
  • 转载请务必保留本文链接:https://go.coder-hub.com/76627747.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定