mercredi 21 août 2019

How to use conditional functions to transform text values into numeric values?

I have a dataframe with two columns that contain text values, i want to add a new column that returns a score or numerical value, depending on the text of the two previous columns.

Im fairly new to R and been trying to practice and play around a bit in order to get familiarized with it.

I have the following dataframe built from 2 vectors NP_range <- c("AOE",ST" and NP_type <- c("Buster", "Arts", "Quick")

NP_range NP_type

1 AOE Buster

2 AOE Buster

3 ST Buster

4 ST Arts

5 AOE Buster

6 ST Quick

7 AOE Buster

8 AOE Arts

Ive been trying to use the following code:

EXAMPLE$NP_Score <- ifelse(EXAMPLE$NP_range = "AOE" & EXAMPLE$NP_type = "Buster", 10, ifelse(EXAMPLE$NP_range == "AOE" & EXAMPLE$NP_type = "Buster", 7, ifelse(EXAMPLE$NP_range = "AOE" & EXAMPLE$NP_type == "Buster", 5, ifelse(EXAMPLE$NP_range == "AOE" & EXAMPLE$NP_type == "Buster", 3))))

Have looked around but cannot find an appropiate answer that helps me do what i want with the code.

What i want is a new column NP_Score that assigns a value (10,5,3, etc...) according to the text contained on columns NP_type and NP_range I end up with the following error:

Error: unexpected '=' in "EXAMPLE$NP_Score <- ifelse(EXAMPLE$NP_range ="

Aucun commentaire:

Enregistrer un commentaire