Edited to ask a better question:
libary("ggplot2)
data("diamonds")
diamonds_small <- diamonds[1:5000, ]
cutnewfunc <- function(x){
ifelse(x == c("Ideal", "Premium", "Very Good", "Good", "Fair"), c("Above Average", "Above Average", "Very Good", "Below Average", "Below Average"), "Default")
}
cutvect = as.vector(diamonds_small$cut)
newdiamonds = cutnewfunc(cutvect)
newdiamonds
Result:
[1] "Above Average" "Above Average" "Default" "Default" "Default" "Default" "Default"
[8] "Very Good" "Default" "Default" "Default" "Default" "Default" "Default"
[15] "Default" "Default" "Default" "Default" "Below Average" "Default" "Default"
As you can see, there are a lot of "Default" values, which shouldn't be happening, as calling cutvect will result in:
[1] "Ideal" "Premium" "Good" "Premium" "Good" "Very Good" "Very Good" "Very Good" "Fair"
[10] "Very Good" "Good" "Ideal" "Premium" "Ideal" "Premium" "Premium" "Ideal" "Good"
[19] "Good" "Very Good" "Good" "Very Good" "Very Good" "Very Good" "Very Good" "Very Good" "Premium"
How would I go about comparing every value, and not result in "default"?
Thank you!
I need to compare a string value inside a data frame, generate result accordingly, and add it to a new column in the data frame.
I think I'll need to: 1. take the column with the parameter strings and form a vector, 2. Compare the strings and form a new vector with the result, 3. add it the result vector as a new column to the original data frame.
The comparing strings bit is not working for me.
The question says I need to use the 'ifelse' function, but comparing string result in 'only applicable to logical or numeric values' error.
Can anyone help?
Thank you!