简体   繁体   中英

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.

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. I have scraped both locations and created 2 variables from them, which I now want to combine to 1 vector.

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. How can I do that 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. 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

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)

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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