[R] levels

Marc Schwartz m@rc_@chw@rtz @end|ng |rom me@com
Wed Jul 15 18:16:48 CEST 2020


> On Jul 15, 2020, at 4:31 AM, andy elprama <andy.elprama using gmail.com> wrote:
> 
> Dear R-users,
> 
> Something strange happened within the command "levels"
> 
> R version 3.6.1
> name <- c("a","b","c")
> values <- c(1,2,3)
> data <- data.frame(name,values)
> levels(data$name)
> [1] "a" "b" "c"
> 
> R version 4.0
> name <- c("a","b","c")
> values <- c(1,2,3)
> data <- data.frame(name,values)
> levels(data$name)
> [1] NULL
> 
> What is happening here?


Hi,

The default value for 'stringsAsFactors' for data.frame() and read.table() changed from TRUE to FALSE in version 4.0.0, per the news() file:

"R now uses a stringsAsFactors = FALSE default, and hence by default no longer converts strings to factors in calls to data.frame() and read.table()."


Using 4.0.2:

data <- data.frame(name, values, stringsAsFactors = TRUE)

> levels(data$name)
[1] "a" "b" "c"


If you see behavioral changes from one version of R to another, especially major version increments, check the news() file.

Regards,

Marc Schwartz

 


More information about the R-help mailing list