- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# How to deal with error “var(x) : Calling var(x) on a factor x is defunct.” in R?

The error “Calling var(x) on a factor x is defunct” occurs when we try to apply a numerical function on factor data.

For example, if we have a factor column in a data frame then applying numerical functions on that column would result in the above error. To deal with this problem, we can use as.numeric function along with the numerical function as shown in the below examples.

## Example 1

Following snippet creates a sample data frame −

x<-factor(rpois(20,5)) df<-data.frame(x) df

## Output

The following dataframe is created −

x 1 7 2 3 3 7 4 4 5 7 6 6 7 4 8 6 9 8 10 2 11 6 12 6 13 9 14 5 15 3 16 4 17 10 18 2 19 4 20 2

Now, in order to apply t test on data in x, add the following code to the above snippet −

t.test(df$x,mu=2)

## Output

If you execute all the above given snippets as a single program, it generates the following Output −

Error in var(x) : Calling var(x) on a factor x is defunct.

Hence, use something like the following ' to test for a constant vector.

'all(duplicated(x)[-1L])

## Output

If you execute all the above given snippets as a single program, it generates the following Output −

In addition: Warning message: In mean.default(x) : argument is not numeric or logical: returning NA

Now, use as.numeric function on x while applying t.test function and add the following code to the above snippet −

t.test(as.numeric(df$x),mu=2)

## Output

If you execute all the above given snippets as a single program, it generates the following Outpu for the one sample t-test −

data: as.numeric(df$x) t = 4.3061, df = 19, p-value = 0.0003811 alternative hypothesis: true mean is not equal to 2 95 percent confidence interval: 3.156355 5.343645 sample estimates: mean of x 4.25

## Example 2

Following snippet creates a sample data frame −

y<-factor(rpois(20,2)) dat<-data.frame(y) dat

The following dataframe is created −

y 1 1 2 2 3 4 4 4 5 2 6 0 7 0 8 1 9 3 10 0 11 2 12 0 13 0 14 2 15 2 16 2 17 2 18 0 19 4 20 2

Now, in order to apply t test on data, add the following code to the above snippet −

t.test(dat$y,mu=0)

## Output

If you execute all the above given snippets as a single program, it generates the following output −

Error in var(x) : Calling var(x) on a factor x is defunct.

Use something like the following to test for a constant vector −

'all(duplicated(x)[-1L])'

## Output

If you execute all the above given snippets as a single program, it generates the following Output −

In addition: Warning message: In mean.default(x) : argument is not numeric or logical: returning NA

Now, use as.numeric function on y while applying t.test function, add the following code to the above snippet −

t.test(as.numeric(dat$y),mu=0)

## Output

If you execute all the above given snippets as a single program, it generates the following Output for the one sample t-test −

data: as.numeric(dat$y) t = 8.5446, df = 19, p-value = 6.216e-08 alternative hypothesis: true mean is not equal to 0 95 percent confidence interval: 2.000878 3.299122 sample estimates: mean of x 2.65

- Related Questions & Answers
- Difference between NumberLong(x) and NumberLong(“x”) in MongoDB?
- Regular expression “[X?+] ” Metacharacter Java
- How to deal with Error: stat_count() can only have an x or y aesthetic in R?
- How to deal with “could not find function” error in R?
- How to deal with error “$ operator is invalid for atomic vectors” in R?
- How to deal with hist.default, 'x' must be numeric in R?
- How to make a jQuery function call after “X” seconds?
- How to insert a row with a timestamp “X days ago” in MySQL?
- How to deal with error “undefined columns selected” while subsetting data in R?
- How to deal with error “undefined columns selected when subsetting data frame” in R?
- How to deal with warning message “Removed X rows containing missing values” for a column of an R data frame while creating a plot?
- How to deal with the error “Error in int_abline---plot.new has not been called yet” in R?
- How to deal with glm.fit error “NA/NaN/Inf” for logistic regression model in R?
- Difference between x++ and x = x+1 in Java
- Count numbers whose sum with x is equal to XOR with x in C++