-1

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!

  • 1
    Welcome to StackOverflow! Please read the info about [how to ask a good question](http://stackoverflow.com/help/how-to-ask) and how to give a [reproducible example](http://stackoverflow.com/questions/5963269). This will make it much easier for others to help you. – Sotos Jan 11 '18 at 14:17
  • Please save the RStudio tag for problems specific to that editor - for example if you have R code that runs fine in the R Gui or command line but doesn't run in RStudio – Gregor Thomas Jan 11 '18 at 14:18

1 Answers1

0

Your problem is here:

x == c("Ideal", "Premium", "Very Good", "Good", "Fair")

== doesn't work the way you seem to think... We can try some simple cases to see what it does:

> x = "Premium"
> x == c("Ideal", "Premium", "Very Good", "Good", "Fair")
[1] FALSE  TRUE FALSE FALSE FALSE


> x = c("Premium", "Fair")
> x == c("Ideal", "Premium", "Very Good", "Good", "Fair")
[1] FALSE FALSE FALSE FALSE FALSE
Warning message:
In x == c("Ideal", "Premium", "Very Good", "Good", "Fair") :
  longer object length is not a multiple of shorter object length
## Notice this result is ALL FALSE!


> x = rep("Premium", 10)
> x == c("Ideal", "Premium", "Very Good", "Good", "Fair")
 [1] FALSE  TRUE FALSE FALSE FALSE FALSE  TRUE FALSE FALSE FALSE

When we do x == y, if x and y are the same length the comparison will be pairwise: the first element of x compared to the first of y, the second of x compared to the second of y, etc. If one of x or y is longer than the other, the shorter one will be repeated until the lengths are equal, but the comparison is always pairwise.

A full answer on better ways to do combine factor levels is here: Cleaning up factor levels (collapsing multiple levels/labels). The best ways do not use ifelse. But if you must use ifelse, do it like this:

cutnewfunc <- function(x){
  ifelse(x %in% c("Ideal", "Premium"), "Above Average",
    ifelse(x %in% c("Very Good"), "Very Good"), "Default"))
}

I didn't write it out completely because it sounds like it's homework. I'll let you fill in the rest.

Gregor Thomas
  • 104,719
  • 16
  • 140
  • 257