【问题标题】:Replace Value in a Column Using a Loop and Custom Function - R使用循环和自定义函数替换列中的值 - R
【发布时间】:2019-02-21 23:19:53
【问题描述】:

我有一个带有列(名为“颜色”)的 data.frame,其中每个值都是“黑色”。我还创建了一个函数,可以根据另一列的值(“增长”列值)将“黑色”替换为其他颜色。我需要创建一个循环,使用此函数根据“增长”值替换“颜色”列中的值

# Create a function
check_it <- function(x) 
  if(x>500){
    return("green")
  } else if(x<0) {
      return("red")
  } else {
    return("blue")
}
# Create a loop using check_it
for(x in 1:nrow(all_data)) {
     ...
# Given this hint:
# You can use 1:nrow(all_data) as a set of indices 
# to do something like the following inside the loop:
#    all_data[i, "color"] <- 
#    check_it( all_data[i, "growth"] )

有什么建议吗?

样本数据

| station_id | timestamp | growth.x | growth.y | color |
--------------------------------------------------------
|     DB1    | 1/14/01   | 59.916   | 59.9164  | black |
--------------------------------------------------------
|     DB1    | 1/14/02   | 316.128  | 316.128  | black |
--------------------------------------------------------
|     DB1    | 1/14/03   | -12.456  | -12.456  | black |
--------------------------------------------------------
|     DB1    | 1/14/04   | 537.443  | 537.443  | black |
--------------------------------------------------------

感谢您的帮助!感谢 cmets,我能够理解如果没有插入正确的参数(我只有“x”),我的函数将无法工作,并且没有告诉我的函数在哪里寻找“增长”值。

这是我最终使用的代码:

check_it <- function(x, ) 
  if(all_data[x, "growth.x"] >500){
    return("green")
  } else if(all_data[x, "growth.x"] <0) {
      return("red")
  } else {
    return("blue")
}

# Create a loop using check_it
for(x in 1:nrow(all_data)) {

   all_data[x, "color"] <- check_it(x, all_data)
}

【问题讨论】:

  • 请将问题编辑为 1) 只需复制并粘贴代码并删除图像。 2)添加样本数据
  • 您提到您尝试了几个选项,所以我认为展示您尝试的内容会很棒。此外,为了让可能会尝试帮助您的人更容易理解,请提供一些数据样本。
  • 请提供可重现的例子
  • transform(data, color = cut(growth.y ,c(-Inf,-1,500,Inf),c('red','blue','green')))
  • @Onyambu - 不错的解决方案,但这似乎是编写基本函数和循环的练习。

标签: r


【解决方案1】:

当然,您的问题有很多解决方案。但是由于您特别要求一个循环并提供您自己的功能,所以我试图尽可能地坚持您所做的事情。但是你有两个growth-columns,所以我选择了growth.y

datf <- read.table(text="
                        station_id   timestamp   growth.x   growth.y   color  
                        DB1      1/14/01     59.916     59.9164    black  
                        DB1      1/14/02     316.128    316.128    black  
                        DB1      1/14/03      12.456     12.456    black  
                        DB1      1/14/04     537.443    537.443    black", 
                        header = TRUE, stringsAsFactors = FALSE) 

#I had to change your function a little:

check_it <- function(x, dat) 
  if(dat[x, "growth.y"] >500){
    return("green")
  } else if(dat[x, "growth.y"] < 0) {
    return("red")
  } else {
    return("blue")
  }

现在您的循环变量 x 对应于 data.frame 的行索引,并且您正在循环遍历它。在此之前,情况并非如此,您只是将一个数字传递给您的函数。

#And finally the loop
for(x in 1:nrow(datf)){

  datf[x, "color"] <- check_it(x, datf)

}

> datf
  station_id timestamp growth.x growth.y color
1        DB1   1/14/01   59.916  59.9164  blue
2        DB1   1/14/02  316.128 316.1280  blue
3        DB1   1/14/03   12.456  12.4560  blue
4        DB1   1/14/04  537.443 537.4430 green

但是,您应该考虑查看 *apply-function 系列。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-02-17
    • 1970-01-01
    • 2015-08-08
    • 2019-08-08
    • 2018-08-17
    • 1970-01-01
    • 2021-03-23
    • 1970-01-01
    相关资源
    最近更新 更多