更新数据框列中的值的子集 [英] Update subset of values in a dataframe column

查看:69
本文介绍了更新数据框列中的值的子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的数据框的摘录:

Here's an excerpt of my dataframe:

  x         y     se
  4         a     7.146329
 15         a     8.458633
 17         a     9.286849
 11         b     6.700024
  8         b     4.697962  
 12         c     7.884244
 10         c     7.834816
 17         c     7.762385
 12         d     5.910785
 15         d     12.98158

我需要更新第一列,以便将每个数字相减按1,但用于条件a和b。也就是说,我会得到而不是 c(4,15,17,11,11,8,12,10,17,12,15) c(3,14,16,10,7,12,12,10,17,12,15)

I need to update the first column, so that each number will be subtracted by 1, but only for conditions a and b. That is, instead of c(4, 15, 17, 11, 8, 12, 10, 17, 12, 15), I would get c(3, 14, 16, 10, 7, 12, 10, 17, 12, 15).

推荐答案

可以在此处使用 ifelse 。假设数据帧命名为 df1

Could use ifelse here. Assuming data frame is named df1:

df1$x <- ifelse(df1$y %in% c("a", "b"), df1$x - 1, df1$x)

这篇关于更新数据框列中的值的子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆