lundi 2 mars 2020

Searching for words of a column in the texts of another column

I have a table of 1500*6 and a column of 3000 (Seg.name). you see parts of them here.

I need to find the rows in R.name column in the table that have a word in Seg.name column. For instance, it searches "TRUE" in R.name column to find if this word has occurred in any of the rows in R.name. If finds it, then replaces the count with TRUE, otherwise leaves count as NA. Thats why I have two For loops.

I have also my ifelse and for loop part of the code. The code works with no error but it only saves a letter in count column! for instance instead of Pemex, it has P and so one. What do you think is happening here?

**Year  Rystad Operator Names    Country**
2018    Pemex                    Mexico
2018    Other partner(s) US      United States
2018    PetroChina               China
2018    Equinor                  Norway
2018    Saudi Aramco             Saudi Arabia

Seg.name

TRUE
ZYNERGY
ZUEITINA
ZINSZER
ZENITH

for loop code:

for (j in 1:1000)

{
 for (i in 1:20)
{
test=mapply(grepl, pattern=data.s$Seg.name[j], x=data.r$R.name[i], fixed=TRUE)
if (test == TRUE) data.r$count[i]=data.s$Seg.name[j] else {
    if (data.r$count[i]=="NA")  data.r$count[i]="NA" else data.r$count[i]=data.r$count[i]
}        
}
}
return(data.r)

data.r returned in this format:

**Year  R.name                   Country            count   pass**
2018    Pemex                     Mexico               P    NA
2018    Other partner(s) US       United States        S    NA
2018    PetroChina                China                P    NA
2018    Equinor                   Norway               NA   NA
2018    Saudi Aramco              Saudi Arabia         S    NA

Aucun commentaire:

Enregistrer un commentaire