简体   繁体   English

可选的更有效的循环选项

[英]Alternative More Efficient Loop options

Are there any bug-free and faster alternatives to this for loop code? 循环代码是否有任何无错误且更快的替代方法?

for(i in 1:length(Mergedf)) 
{if (Mergedf[i,"z"]==0) 
{Mergedf[i,"cntry_origin"] <-Mergedf[i,"V2"]} 
  print(Mergedf[i,"cntry_origin"])
}

Mergdf if the result of a complex logic. Mergdf如果是复杂逻辑的结果。

Thanks 谢谢

What you're attempting to do is a very basic R 'subset and replace' problem. 您尝试做的是一个非常基本的R “子集和替换”问题。 If you're used to writing in a language like VBA then a for loop seems natural for this. 如果您习惯用VBA类的语言编写,那么for循环似乎很自然。 However, where R "excels" (pun intended) is the ability to vectorise these kinds of operations so it does it in one step, without the need to loop through the entire data set. 但是, R “擅长”(旨在使用双关语)是对这些类型的操作进行矢量化的功能,因此它只需一步即可完成,而无需遍历整个数据集。

The code here is all written in Base R 此处的代码全部用Base R编写

Consider the example data 考虑示例数据

set.seed(1)
MergedDF <- data.frame("z" = c(0,1,2,3,0,1,2,3),
                       "cntry_origin" = letters[1:8],
                       "V2" = rnorm(8,0,1),
                       stringsAsFactors = FALSE)

#   z cntry_origin         V2
# 1 0            a -0.6264538
# 2 1            b  0.1836433
# 3 2            c -0.8356286
# 4 3            d  1.5952808
# 5 0            e  0.3295078
# 6 1            f -0.8204684
# 7 2            g  0.4874291
# 8 3            h  0.7383247

Filtering a data.frame for a given condition 过滤给定条件的data.frame

To get all the rows where z == 0 获取z == 0所有行

MergedDF[MergedDF$z == 0, ]

#   z cntry_origin         V2
# 1 0            a -0.6264538
# 5 0            e  0.3295078

Selecting specific columns 选择特定的列

To get all the values in column cntry_origin where z==0 , there are two equivalent statements: 要获取z==0 cntry_origin列中的所有值,有两个等效的语句:

MergedDF[MergedDF$z == 0, "cntry_origin"]
# [1] "a" "e"

## Or
MergedDF[MergedDF$z == 0, ]$cntry_origin
# [1] "a" "e" 

To get the value of column V2 where z==0 获取z==0 V2列的值

MergedDF[MergedDF$z == 0, ]$V2
#[1] -0.6264538  0.3295078

Replacing one column with different values 用不同的值替换一列

To replace the column cntry_origin with V2 , where z==0 it's just a matter of assigning one to the other cntry_origin V2替换cntry_origin列,其中z==0 ,只需将一个分配给另一个

MergedDF[MergedDF$z == 0, ]$cntry_origin <- MergedDF[MergedDF$z == 0, ]$V2

MergedDF 合并DF

#   z       cntry_origin         V2
# 1 0 -0.626453810742332 -0.6264538
# 2 1                  b  0.1836433
# 3 2                  c -0.8356286
# 4 3                  d  1.5952808
# 5 0  0.329507771815361  0.3295078
# 6 1                  f -0.8204684
# 7 2                  g  0.4874291
# 8 3                  h  0.7383247

The equivalent data.table code would be 等效的data.table代码将是

library(data.table)
setDT(MergedDF)[z==0, cntry_origin := V2]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM