adding columns to dataframe based on the values in existing

Im new to R and have tried to add columns in a certain way in a dataframe without succes.
I have data similiar to the following:

Favorite<-c("Apple","Lemon","Orange","Lemon","Onion", "Apple","")
PersonID<-c(67,82,67,21,02,12,45)
all_Data<-data.frame(PersonID,Favorite)

head(all_Data)
PersonID Favorite
1 67 Apple
2 82 Lemon
3 67 Orange
4 21 Lemon
5 2 Onion
6 12 Apple

I want to add 3 more columns: Country, City and Continent.
so if all_Data$Favorite = Apple then the Country=France, City= Paris and Continent= Europe.
and if all_Data$Favorite=Lemon or Orange then the Country=Greece, City=Athens and Continent=Europe
if all_Data$Favorite=Onion then the Country=Kenya, City=Nairobi and Continent=Africa

I have tried the following but it does not work.

all_Data$Country<- NA
all_Data$City<- NA
all_Data$Continent<-NA

if (all_Data$Favorite=="Apple") {
all_Data$Country<-c("France") |all_Data$City<-c("Paris"| all_Data$Continent="Europe")
} else if (all_Data$Favorite=="Lemon"| all_Data$Favorite=="Orange"){all_Data$Country="Greece" | all_Data$City="Athens"| all_Data$Continent="Europe"
} else {all_Data$Country="Kenya"| all_Data$City="Nairobi"| all_Data$Continent="Africa"}

I need a solution that can be generalized to a large number of variables
How can I do this?
Thanks in advance

Hi Hope it helps

Favorite<-c("Apple","Lemon","Orange","Lemon","Onion", "Apple","")
PersonID<-c(67,82,67,21,02,12,45)
all_Data<-data.frame(PersonID,Favorite)

all_Data['Country']<-as.character()
all_Data['City']<-as.character()
all_Data['Continent']<-as.character()

for (i in 1: nrow(all_Data)){
if (all_Data$Favorite[i]=="Apple") {
mcountry<-"France"
mcity<-"Paris"
mcontinent="Europe"
} else if (all_Data$Favorite[i]=="Lemon" | all_Data$Favorite[i]=="Orange"){
mcountry<-"Greece"
mcity<-"Athens"
mcontinent="Europe"
} else {
mcountry<-"Kenya"
mcity<-"Nairobi"
mcontinent="Africa"
}
all_Data$Country[i]<-mcountry
all_Data$City[i]<-mcity
all_Data$Continent[i]<-mcontinent
}

I think this is a better (more human-readable) way to do it

library(tidyverse)

all_Data <- data.frame(
  stringsAsFactors = FALSE,
          PersonID = c(67, 82, 67, 21, 2, 12, 45),
          Favorite = c("Apple","Lemon","Orange",
                       "Lemon","Onion","Apple",NA)
)

all_Data %>% 
    mutate(
        Country = case_when(
            Favorite == "Apple" ~ "France",
            Favorite %in% c("Lemon", "Orange") ~ "Greece",
            TRUE ~ "Kenya"),
        City = case_when(
            Favorite == "Apple" ~ "Paris",
            Favorite %in% c("Lemon", "Orange") ~ "Athens",
            TRUE ~ "Nairobi"),
        Continent = case_when(
            Favorite %in% c("Apple", "Lemon", "Orange") ~ "Europe",
            TRUE ~ "Africa")
        )
#>   PersonID Favorite Country    City Continent
#> 1       67    Apple  France   Paris    Europe
#> 2       82    Lemon  Greece  Athens    Europe
#> 3       67   Orange  Greece  Athens    Europe
#> 4       21    Lemon  Greece  Athens    Europe
#> 5        2    Onion   Kenya Nairobi    Africa
#> 6       12    Apple  France   Paris    Europe
#> 7       45     <NA>   Kenya Nairobi    Africa

Created on 2022-04-24 by the reprex package (v2.0.1)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.