I have a dataframe ABC which contains 2 columns and rows will differ as I update the table in due course.
PORT COUNTRY
ROTTERDAM NETHERLANDS
MUMBAI INDIA
DAKAR SENEGAL
ROTTERDAM NETHERLANDS
ZHOUSHAN CHINA
ZHENJIANG CHINA
VIZAG INDIA
and so on. It is an extensive list.
In another dataframe, data I created, the PORT column already exists, but I intend to add the COUNTRY column, so that future analysis becomes easy.
I creted the blanks column by the following code
data$COUNTRY <- 0
I intend to write a function so as to match the PORT and COUNTRY in the ABC and fill up the COUNTRY column automatically in data.
I have used the merge() function using the following code
data <- merge(data, ABC, by = "PORT", all.x = TRUE)
Error: Error: No common variables. Please specify `by` param.
I have also tried using the inner_join() in the dplyr package. Error : index out of bounds.
Finally, I also tried to write a function so as to load both data frames, compare and fill up data
fillCountry <- function(x)
{
countrySize <- length(1 : ABC$PORT)
for (i in 1:countrySize)
data$country <- if (ABC$PORT[i] == ABC$COUNTRY[i])
return(data)
}
Error: Error in Ops.factor(ABC$PORT[i], ABC$COUNTRY[i]) :
level sets of factors are different
Please suggest any tool or method and please correct me if I am doing it totally wrong.
Aucun commentaire:
Enregistrer un commentaire