I have some dataframes with more than 3000 columns in each, and I want to bind them together.
When I use
library(dplyr)
bind_rows(dataframe1, dataframe2, dataframe3, dataframe4)
I get a lot of warnings:
In bind_rows_(x, .id) : Unequal factor levels: coercing to character
...
I guess it's because a column has data of type factor in one dataframe and data of type character in another dataframe. But how can I solve this problem?
I know I can use
sapply(dataframe1, class)
to get the classes of a dataframe, but as there are many columns, it is impossible to go through them all in all 4 dataframes.
This seems to be a problem about the data, but what does it mean that something has type factor? Is it a number?
Perhaps start with ?factor about what factors are.
To avoid the warnings, you either use supressWarnings, or you will need to convert to character first. For example (untested):
library(tidyverse)
l <- list(dataframe1, dataframe2, dataframe3, dataframe4)
map_dfr(l, ~mutate(., across(where(is.factor), as.character))
Related
I'm working on importing a data set which has a column with categories "PR","CG","SH","CF","SC","PI","PA". However, some rows have multiple values (e.g. PR,CG). I was able to split those strings into lists using FFG=str_split(FFG,pattern=","), but when I try to factor using
df<-df%>%(FFG=col_factor(levels=c("PR","CG","SH","CF","SC","PI","PA"))) I get "Error in function_list[k] : attempt to apply non-function" back. I'm new to R so if I missed any important information, just let me know. Any advice would be incredibly helpful, thank you!
One option is to use separate_rows to split the 'FFG' column and then convert to factor with levels specified
library(dplyr)
library(tidyr)
df %>%
separate_rows(FFG, sep=",") %>%
mutate(FFG = factor(FFG, levels=c("PR","CG","SH","CF","SC","PI","PA")))
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"
I know how to convert one factor of a dataframe to numeric:
rds$fcv12afa3num <- as.numeric(levels(rds$fcv12afa3))[rds$fcv12afa3]
My two questions:
But how can I convert all dataframe-columns simultaneously, if the df consists only of factors?
How can I convert several factors simultaneously, based on a pattern of the column name?
I have many NA's, if that matters.
Thanks for your answer, Christian
Without example data, I can't give a completely exact answer, but this should get you started.
factorVars <- names(YourData)[vapply(YourData, is.factor, logical(1))]
YourData[, factorVars] <- lapply(YourData[, factorVars, drop = FALSE],
as.numeric)
Some notes:
Use drop = FALSE to handle the case of there only being one factor in your data frame.
If all of the factors are data frames, you may get a list object in return. You'd have to run that list through as.data.frame to get your data frame back.
I have a simple problem. I have a data frame with 121 columns. columns 9:121 need to be numeric, but when imported into R, they are a mixture of numeric and integers and factors. Columns 1:8 need to remain characters.
I’ve seen some people use loops, and others use apply(). What do you think is the most elegant way of doing this?
Thanks very much,
Paul M
Try the following... The apply function allows you to loop over either rows, cols, or both, of a dataframe and apply any function, so to make sure all your columns from 9:121 are numeric, you can do the following:
table[,9:121] <- apply(table[,9:121],2, function(x) as.numeric(as.character(x)))
table[,1:8] <- apply(table[,1:8], 2, as.character)
Where table is the dataframe you read into R.
Briefly I specify in the apply function the table I want to loop over - in this case the subset of your table we want to make changes to, then we specify the number 2 to indicate columns, and finally give the name of the as.numeric or as.character functions. The assignment operator then replaces the old values in your table with the new ones of correct format.
-EDIT: Just changed the first line as I recalled that if you convert from a factor to a number, what you get is the integer of the factor level and not the number you think you are getting to factors first need to be converted to characters, then numbers, which was can do just by wrapping as.character inside as.numeric.
When you read in the table use strinsAsFactors=FALSE then there will not be any factors.
I am having trouble turning my data.frame into a matrix format. Because I wanted to change my data.frame with mostly factor variables into a numeric matrix, I used the following code
UN2010frame <- data.matrix(lapply(UN2010, as.numeric))
However when I checked the mode of the UN2010frame, it still showed up as a list. Because the code I want to run (Ordrating) does not accept data in a list format, I used UN2010matrix <- unlist(UN2010frame) to unlist my matrix. When I did this, my first row ( which was formerly a row with column names) turned into NAs. This was a problem for me because when I tried to run an ordinal IRT model using this data set, I got the following error message.
> Error in 1:nrow(Y) : argument of
> length 0
I think it is because all the values in my first row are now gone.
If you could help me on any front, It would be deeply appreciated.
Thank you very much!
Haillie
First, the correct use of data.matrix is :
data.matrix(UN2010)
as it converts automatically to numeric. The lapply in your code is the first source for the error you get. You put a list in the data.matrix function, not a dataframe. So it returns a list of matrices, and not a matrix.
Second, unlist returns a vector, not a matrix. So pretty sure you won't find a "first row with NA", as you have a vector. Which might explain part of your confusion.
You probably have a character column somewhere. Converting this to numeric gives NA. If you don't want this, then exclude them from the further analysis. One possibility is to use colwise() from the plyr package to convert only the factors:
colwise(as.numeric,is.factor)(UN2010)
Which returns a dataframe with only the factors. This can be easily converted by data.matrix() or as.matrix(). Alternatively you use the base solution :
id <- sapply(UN2010,is.character)
sapply(UN2010[!id],as.numeric)
which will return you a matrix with all non-character columns converted to numeric.If you really want to keep the dataframe with all original columns, you can do :
UN2010frame <- UN2010
UN2010frame[!id] <- lapply(UN2010[!id],as.numeric)
Toy example code :
UN2010 <- data.frame(
F1 = factor(rep(letters[1:3],10)),
F2 = factor(rep(letters[5:10],5)),
Char = rep(letters[11:16],each=5),
Num = 1:30,
stringsAsFactors=FALSE
)
Try as.data.frame instead of data.matrix.