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 I R?
My output should look like this:
vect_3
[1] Description 1
[2] Description 2
[3] Description 3
Many thanks in advance!
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.