how to convert numeric column to factor in R - r

I'm trying to use the softImpute command (from the softImpute package) for filling in missing values, and I'm trying to turn categorical variables in a large data frame into factor type before using the softImpute.
I've used as.factor command and factor command but they all yield the following
train[a]=factor(train[a])
Error in sort.list(y) : 'x' must be atomic for 'sort.list'
Have you called 'sort' on a list?
a here is a vector like: c(1:92)
I tried as.character too but the softImpute command would not recognize the variables as character and would treat them as numeric, resulting in decimal values for categorical/indicator variables.

Try:
train[[a]]=factor(train[[a]])
This does assume, of course that ,a is an object with either a numerical value in the range 1:length(train) or is one of the values in the names(train) vector. If you reference a dataframe using "[" you get a list with one element which happens to be the vector you were hoping to "factorize" but it isn't really a vector but is rather a one element list. The "[[" function instead gives you just the vector.

Related

How to use 'as.factor' with 'apply'?

I tried to convert the categorical features in a dataset to factors. However, using apply with as.factor did not work:
convert <- c(2:5, 7:9,11,16:17)
read_file[,convert] <- data.frame(apply(read_file[convert], 2, as.factor))
However, switching to lapply did work:
read_file[,convert] <- data.frame(lapply(read_file[convert], as.factor))
Can someone explain to me what's the difference and why second code works while the first fails?
apply returns a matrix and a matrix cannot contain a factor variable. Factor variables are coerced to character variables if you create a matrix from them. The documentation in help("apply") says:
In all cases the result is coerced by as.vector to one of the basic
vector types before the dimensions are set, so that (for example)
factor results will be coerced to a character array.
lapply returns a list and a list can contain (almost) anything. In fact, a data.frame is just a list with some additional attributes. You don't even need to call data.frame there. You can just subset-assign a list into a data.frame.

`Error $ is invalid for atomic vectors` when replacing columns in a dataset in R

I'm trying to replace a column in my dataset in RStudio so I used this code:
weekday <- weekdays(as.Date(x$Y))
but I keep getting an error message saying Error in x$Y: $ is invalid for atomic vectors. Please how do I resolve this?
An atomic vector is a simple vector of one of the basic vector types: "logical, integer, real, complex, string (or character) and raw".1 An atomic vector can have multiple values, but always of the same type. Data frame columns are an example of atomic vectors.
A list (a.k.a. generic vector) is a different data type; it can hold multiple vectors of different types. Examples of lists are list, data.frame, data.table, etc.
The $ is an operator to take a subset of a list.2 It can only be used on lists, not on atomic vectors.
Basically, R is complaining that x$Y is invalid, that x is an atomic type, and as such the $ operator has no meaning on it.
You say you are trying to replace a column in your "data set"; I'm assuming you have a data frame, with a column of character strings that represent dates. However, x is apparently not the name of that data frame.
https://cran.r-project.org/doc/manuals/r-devel/R-lang.html#Vector-objects
https://cran.r-project.org/doc/manuals/r-devel/R-lang.html#Operators

Error while executing the pairs function in R

I am getting an error while executing the exact same code given in my textbook on my machine. It is the simple pairs function code.
my code is pairs(college[, 1:10])
The error I am getting:
Error in pairs.default(college[, 1:10]) : non-numeric argument to
'pairs'
Your college[,1:10] dataset contain columns that are not numeric.
Run:
str(college[,1:10])
And inspect the column types in your dataframe.
Since pairs matrix essentially creates a matrix of scatterplot using each combination of columns, it expect a dataframe with numeric columns only.
This makes sense, if you consider the fact that you wouldn't create a scatterplot using Student Gender (a categorical variable, i.e non-numeric) against Student Age for example. It doesn't make sense.
In your case, this error:
Error in pairs.default(college[, 1:10]) : non-numeric argument to 'pairs'
Tells you that among the 10 columns, one or more of these are non-numeric. Either remove these columns in your call to pairs() or perform explicit coercion using as.numeric().

Error: Must subset columns with a valid subscript vector. x Can't convert from <double> to <integer> due to loss of precision

I am working on a data frame with all variables of numeric type
summary.default(pfnew)
ID 6016315 -none- numeric
iterator 6016315 -none- numeric
value 6016315 -none- numeric
CV 6016315 -none- numeric
I want to create a pivot table grouped by iterator and CV and summarize the count of ID. In essence, I want number of points in the data frame corresponding to a particular set of iterator and CV value. The code I have used is:
Code
install.packages("tidyr")
install.packages("dplyr")
install.packages("vctrs")
library(vctrs)
library(tidyr)
library(dplyr)
allow_lossy_cast(pivot<-pfnew%>%
select(pfnew$iterator,pfnew$CV,pfnew$ID)%>%
summarise(CT=count(pfnew$ID)))
But as discussed in other forums even after using allow_lossy_cast, I am getting the same error message.
Error: Must subset columns with a valid subscript vector. x Can't convert from to due to loss of precision.
How can we resolve this? Or can we do the same job in any other manner?
I just came across the same error with a different dplyr function and realized that I included the name of the data frame after calling it. Try removing pfnew$ from select and summarise so it's select(c(iterator, CV,ID)).
select function throws error when you are using dataframe to call the predictors here. Try renaming the column name to a more suitable name in case it persists (using space in name will throw error if you donot use dataframe to call the predictor column, therefore avoid using spaces in your column name) and use the predictors name directly and this will resolve the issue.
Example - instead of pfnew$This is an example,
use pfnew$This_Is_an_example
and then directly use this name in select -
select(This_Is_an_example) %>%
....

Selecting unique values from single column of a data frame

I have a data frame consisting of five character variables which represent specific bacteria. I then have thousands of observations of each variable that all begin with the letter K. eg
x <- c(K0001,K0001,K0003,K0006)
y <- c(K0001,K0001,K0002,K0003)
z <- c(K0001,K0002,K0007,K0008)
r <- c(K0001,K0001,K0001,K0001)
o <- c(K0003,K0009,K0009,K0009)
I need to identify unique observations in the first column that don't appear in any of the remaining four columns. I have tried the approach suggested here which I think would work if I could create individual vectors using select ...
How to tell what is in one vector and not another?
but when I try to create a vector for analysis using the code ...
x <- select(data$x)
I get the error
Error in UseMethod("select_") :
no applicable method for 'select_' applied to an object of class "character
I have tried to mutate the vectors using as.factor and as.numeric but neither of these approaches work as the first gives an equivalent error as above, and as.numeric returns NAs.
Thanks in advance
The reference that you cited recommended using setdiff. The only thing that you need to do to apply that solution is to convert the four columns into one, so that it can be treated as a set. You can do that with unlist
setdiff(data$x, unlist(data[,2:5]))
"K0006"

Resources