R: Adding a row to a dataframe with multiple classes

I have a seemingly simple task of adding a row to a data frame in R, but I just can't do it!

I have a data frame with 50 rows and 100 columns. The data frame that I would like to keep in the same format has the first column as a factor, and all other columns as characters are what was produced. I just wanted to add append 51st row ... but every time I get warnings.

My added data is in the form Cat <- c("Cat", 1,NA,3,NA,5) . (I don’t know where "or" need to go "- brand new to R!)

rbind each time displays "invalid factor levels."

eg. df <- rbind(df,Cat)

I believe this is due to factor / symbol division.

+6
source share
1 answer

The factor levels in your data.frame should also contain the values ​​in your "Cat" object for the corresponding factor column.

Here is a simple example:

 df <- data.frame(v1 = c("a", "b"), v2 = 1:2) toAdd <- list("c", 3) ## Warnings... rbind(df, toAdd) # v1 v2 # 1 a 1 # 2 b 2 # 3 <NA> 3 # Warning message: # In `[<-.factor`(`*tmp*`, ri, value = "c") : # invalid factor level, NA generated ## Possible fix df$v1 <- factor(df$v1, unique(c(levels(df$v1), toAdd[[1]]))) rbind(df, toAdd) # v1 v2 # 1 a 1 # 2 b 2 # 3 c 3 

Alternatively, consider the rbindlist from "data.table", which should save you from having to convert factor levels:

 > library(data.table) > df <- data.frame(v1 = c("a", "b"), v2 = 1:2) > rbindlist(list(df, toAdd)) v1 v2 1: a 1 2: b 2 3: c 3 > str(.Last.value) Classes 'data.table' and 'data.frame': 3 obs. of 2 variables: $ v1: Factor w/ 3 levels "a","b","c": 1 2 3 $ v2: num 1 2 3 - attr(*, ".internal.selfref")=<externalptr> 
+4
source

Source: https://habr.com/ru/post/984984/


All Articles