77819

Replace value with Factor in r data.table

I have a data set that has a column for a factor (in the example case, cut). It is currently set up like this:

library(ggplot2) # access to diamonds dataset library(data.table) data <- data.table(diamonds)[,list(mean_carat=mean(carat)), by=c('cut', 'color')]

I am trying to change all entries named "Fair" to "Good" because in my data set, the two entries are actually the same thing but notated differently. The syntax that I have been trying to use is:

data[which(cut = "Fair"), cut := "Good"]

and the output is

>Error: unexpected symbol in "data[which(cut = "Fair"), cut := "Good"]"

Can anyone tell me where I am going wrong?

Answer1:

You used = instead of ==. Try

data[which(cut == "Fair"), cut := "Good"]

You also don't actually need the which statement:

data[cut == "Fair", cut := "Good"]

Recommend

  • ggplot: align plots together and add common labels and legend
  • change the hierarchy of a list in R
  • How to get a character ASCII value in a integer variable?
  • Flipping and maintaining aspect ratio of a chart in ggplot2 [duplicate]
  • making y-axis labels bold in ggplot (x-axis is set bold but y-axis label doesn't change)
  • CultureInfo.InvariantCulture in plain english
  • ddply run in a function looks in the environment outside the function?
  • Generating smoothed randoms that follow a distribution
  • Use of Java generics could be hanging the compiler
  • how to make pdf page to fit to screen size in vfr reader
  • r data.table usage in function call
  • Kafka reassignment of __consumer_offsets incorrect?
  • How do I scale the y-axis on a histogram by the x values in R?
  • Pass … argument to another function
  • R add index column to data frame based on row values
  • Correct way to set color to transparent with matplotlib.pcolormesh()?
  • Apply kurtosis to a distribution in python
  • Where is exactly is the demarkation between a version of Java and the JVM?
  • Get Quantile values from geom_boxplot()
  • MongoDB query comments along with user information
  • rapply over a nested list in R
  • Geom_jitter colour based on values
  • Shouldn't else be indented in the below code
  • Groovy: Unexpected token “:”
  • How to access EntityManager inside Entity class in EJB3
  • Repeat a vertical line on every page in Report Builder / SSRS
  • Delete MySQLi record without showing the id in the URL
  • Apache 2.4 - remove | delete | uninstall
  • Unanticipated behavior
  • Comma separated Values
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Unit Testing MVC Web Application in Visual Studio and Problem with QTAgent
  • Benchmarking RAM performance - UWP and C#
  • Trying to get generic when generic is not available
  • Add sale price programmatically to product variations
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • java string with new operator and a literal
  • How do I use LINQ to get all the Items that have a particular SubItem?