简体   繁体   English

根据R中的指定规则将2个向量组合为1个向量

[英]Combining 2 vectors to 1 vector based on specified rules in R

I have to vectors with the same length [1:216] and I would like to combine them into 1 vector based on some rules. 我必须具有相同长度的矢量[1:216],并且我想根据一些规则将它们组合为1个矢量。

Rationale: I have obtained both vectors from scraping a page for a description. 原理:我从抓取页面进行描述时获得了两个向量。 Now, the description is placed in a box that has no unique name and appears in 2 different places (thus also 2 different selector gadget identifiers) across all my observations. 现在,描述被放置在一个没有唯一名称的框中,并且在我所有的观察结果中出现在2个不同的位置(因此还有2个不同的选择器小工具标识符)。 I have scraped both locations and created 2 variables from them, which I now want to combine to 1 vector. 我已经抓取了两个位置并从中创建了2个变量,现在我想将其合并为1个向量。

This is how the vectors look at the moment: 向量现在是这样的:

vect_1 
[1] Description 1
[2] NA 
[3] Description 3

vect_2 
[1] ""
[2] Description 2
[3] "" 

Thus, my code needs to specify, if NA or "" then take observation from other vector, otherwise use description from this vector. 因此,我的代码需要指定是否为NA或“”,然后从其他向量中进行观察,否则使用该向量中的描述。 How can I do that IR? 我该怎么办IR?

My output should look like this: 我的输出应如下所示:

vect_3 
[1] Description 1
[2] Description 2
[3] Description 3

Many thanks in advance! 提前谢谢了!

Assuming that the vectors are of equal length and that one of the paired elements will always be a text string and the other will always be NA or "", then the following should do. 假设向量的长度相等,并且成对的元素之一始终为文本字符串,而另一个元素始终为NA或“”,则应执行以下操作。 You may have to change it a bit if that's not always the case. 如果并非总是如此,则可能需要稍作更改。

vect_1 <- c("Description 1", NA, "Description 3")
vect_2 <- c("", "Description 2", "")

vect_combined <- ifelse(!is.na(vect_1) & vect_1 != "", vect_1, vect_2)
vect_combined # Print
#> [1] "Description 1" "Description 2" "Description 3"

Kindly go through the following solution: 请通过以下解决方案:

vect_1=c("Description 1",NA,"Description 3")
vect_1
[1] "Description 1" NA              "Description 3"
vect_2=c("","Description 2","")
vect_2
[1] ""              "Description 2" ""             

vect_3=c()                     # Create an empty vector

for(i in 1:length(vect_1)){
  if(is.na(vect_1[i])){        # If value in vect_1 is NA
  vect_3=c(vect_3,vect_2[i])   # Look into vect_2
  }
 else{                         # Else
 vect_3=c(vect_3,vect_1[i])    # Copy value from vect_1
 }
}
vect_3                         # Print vect_3
[1] "Description 1" "Description 2" "Description 3"

Hope it is easier for you to understand. 希望您更容易理解。

Try dplyr::coalesce 尝试dplyr::coalesce

vec1 <- c("Description 1", NA, "Description 3")
vec2 <- c("", "Description 2", "")
dplyr::coalesce(vec1, vec2)
# [1] "Description 1" "Description 2" "Description 3"

The following is safer since "" might be recognized as a meaningful value - use na_if(vec, value) 由于""可能被识别为有意义的值,因此以下方法更安全-使用na_if(vec, value)

dplyr::coalesce(na_if(vec1, ""), na_if(vec2, ""))

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

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