用 R 中的另一个值替换数据框多列中出现的数字 [英] Replacing occurrences of a number in multiple columns of data frame with another value in R

查看:14
本文介绍了用 R 中的另一个值替换数据框多列中出现的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ETA: 顺便说一句,下面的要点是不必遍历我的整个列向量集,以防万一这是一个建议的解决方案(只需执行已知的一次工作一次).

ETA: the point of the below, by the way, is to not have to iterate through my entire set of column vectors, just in case that was a proposed solution (just do what is known to work once at a time).

有很多示例将 R 中数据框的单个向量中的值替换为其他值.

There's plenty of examples of replacing values in a single vector of a data frame in R with some other value.

  • Replace a value in a data frame based on a conditional (if) statement in R
  • replace numbers in data frame column in r [duplicate]

以及如何用其他东西替换 NA 的所有值:

And also how to replace all values of NA with something else:

我正在寻找类似于最后一个问题,但基本上试图用另一个值替换一个值.对于多个列满足一个条件的情况,或者只是尝试对多个列执行前两个问题中的操作,我无法生成映射到我的实际数据框的逻辑值数据框.

What I'm looking for is analogous to the last question, but basically trying to replace one value with another. I'm having trouble generating a data frame of logical values mapped to my actual data frame for cases where multiple columns meet a criteria, or simply trying to do the actions from the first two questions on more than one column.

示例:

data <- data.frame(name = rep(letters[1:3], each = 3), var1 = rep(1:9), var2 = rep(3:5, each = 3))

data
  name var1 var2
1    a    1    3
2    a    2    3
3    a    3    3
4    b    4    4
5    b    5    4
6    b    6    4
7    c    7    5
8    c    8    5
9    c    9    5

假设我希望 var1var24 的所有值都是 10.

And say I want all of the values of 4 in var1 and var2 to be 10.

我确定这是初级的,我只是没有正确考虑它.我一直在尝试这样的事情:

I'm sure this is elementary and I'm just not thinking through it properly. I have been trying things like:

data[data[, 2:3] == 4, ]

那行不通,但如果我用 data[, 2] 而不是 data[, 2:3] 做同样的事情,一切都会正常.似乎逻辑测试(如 is.na())适用于多行/多列,但数值比较效果不佳?

That doesn't work, but if I do the same with data[, 2] instead of data[, 2:3], things work fine. It seems that logical test (like is.na()) work on multiple rows/columns, but that numerical comparisons aren't playing as nicely?

感谢您的任何建议!

推荐答案

您想在整个数据框中搜索与您要替换的值相匹配的任何值.与运行逻辑测试的方式相同,例如用 10.. 替换所有缺失值.

you want to search through the whole data frame for any value that matches the value you're trying to replace. the same way you can run a logical test like replacing all missing values with 10..

data[ is.na( data ) ] <- 10

您也可以将所有 4 替换为 10.

you can also replace all 4s with 10s.

data[ data == 4 ] <- 10

至少我认为这就是你所追求的?

at least i think that's what you're after?

假设您想忽略第一行(因为都是字母)

and let's say you wanted to ignore the first row (since it's all letters)

# identify which columns contain the values you might want to replace
data[ , 2:3 ]

# subset it with extended bracketing..
data[ , 2:3 ][ data[ , 2:3 ] == 4 ]
# ..those were the values you're going to replace

# now overwrite 'em with tens
data[ , 2:3 ][ data[ , 2:3 ] == 4 ] <- 10

# look at the final data
data

这篇关于用 R 中的另一个值替换数据框多列中出现的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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