Reputation: 1
Hi here is a beginner question! I need some help regarding overwrite specific charter label, based on value from Class-column .
If the labels from Class-column says "Sport" with value from Sport-Class. Please see example below.
I tried to use ifelse
statement:
DF$Class<-ifelse((DF$Class %in% "Sport"), DF$SportClass, DF$Class)
I have two columns, And my wanted output is "Column: Expectations"
ID: Class: SportClass: | (Expetions)
1 Movie Hockey | Movie
2 Seriers Fotboll | Series
3 Movie Tennsi | Movie
4 Sport Golf | Golf
Can somebody please assist me?
Upvotes: 0
Views: 35
Reputation: 16121
Make sure you are using the correct column names of your dataframe and that your variables are not factors, because the levels don't match and your process breaks.
Using the following dataframe
DF = read.table(text = "
ID Class SportClass
1 Movie Hockey
2 Seriers Fotboll
3 Movie Tennsi
4 Sport Golf
", header=T, stringsAsFactors=F)
I can successfully update variable/column Class
# dataset before
DF
# ID Class SportClass
# 1 1 Movie Hockey
# 2 2 Seriers Fotboll
# 3 3 Movie Tennsi
# 4 4 Sport Golf
# update variable
DF$Class <- ifelse(DF$Class %in% "Sport", DF$SportClass, DF$Class)
# dataset after
DF
# ID Class SportClass
# 1 1 Movie Hockey
# 2 2 Seriers Fotboll
# 3 3 Movie Tennsi
# 4 4 Golf Golf
Note that you don't really parenthesis around DF$Class %in% "Sport"
and that you can also use DF$Class == "Sport"
in this case.
Upvotes: 1