遍历数据帧并根据条件[R]更改值 [英] Iterating through data frame and changing values on condition [R]
问题描述
必须要记帐,因为这个for循环序列已经困扰了我很长时间了.
Had to make an account because this sequence of for loops has been annoying me for quite some time.
我在R中有一个数据帧,具有1000行和10列,每个值的范围为1:3.我想重新编码每个条目,以便:1 == 3,2 == 2,3 == 1.我知道,有更简便的方法可以做到这一点,例如对每个列进行子设置并对条件进行硬编码,但这并不总是理想的,因为我使用的许多数据集最多有100列.
I have a data frame in R with 1000 rows and 10 columns, with each value ranging from 1:3. I would like to re-code EVERY entry so that: 1==3, 2==2, 3==1. I understand that there are easier ways to do this, such as sub-setting each column and hard coding the condition, but this isn't always ideal as many of the data sets that I work with have up to 100 columns.
我想使用嵌套循环来完成此任务-到目前为止,这是我的目的:
I would like to use a nested loop in order to accomplish this task -- this is what I have thus far:
for(i in 1:nrow(dat_trans)){
for(j in length(dat_trans)){
if(dat_trans[i,j] == 1){
dat_trans[i,j] <- 3
} else if(dat_trans[i,j] == 2){
dat_trans[i,j] <- 2
} else{
dat_trans[i,j] <- 1
}
}
}
所以我遍历第一列,获取每个值并根据if/else的条件对其进行更改,我仍在学习R,因此,如果我的代码中有任何指针,请随时指出.
So I iterate through the first column, grab every value and change it based on the if/else's condition, I am still learning R so if you have any pointers in my code, feel free to point it out.
代码
推荐答案
R是向量化语言,因此您实际上不需要内部循环.
另外,如果您注意到4-旧值" =新值",则可以消除if
语句.
R is a vectorized language, so you really don't need the inner loop.
Also if you notice that 4-"old value" = "new value", you can eliminate the if
statements.
for(i in 1:ncol(dat_trans)){
dat_trans[,i] <- 4-dat_trans[,i]
}
外部循环现在仅跨列迭代10次,而不是所有行1000次.这将大大提高性能.
The outer loop is now iterating across the columns for only 10 iterations as opposed to 1000 for all of rows. This will greatly improve performance.
这篇关于遍历数据帧并根据条件[R]更改值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!