Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

R: Adding row to a dataframe with multiple classes

Tags:

dataframe

r

row

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, which I would like to keep in the same format, has the first column as a factor, and all other columns as characters -- this is what lapply produced. I would simply like to add append a 51st row...but I incur warnings every time.

My added data is of the form Cat <- c("Cat", 1,NA,3,NA,5). (I have no clue where " or ' need to go - quite new to R!)

rbind shows "invalid factor levels" every time.

e.g. df <- rbind(df,Cat)

I believe this is because of the factor/character divide.

like image 966
Guy Avatar asked Apr 11 '15 11:04

Guy


1 Answers

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

Here's 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 rbindlist from "data.table", which should save you from having to convert the 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> 
like image 82
A5C1D2H2I1M1N2O1R2T1 Avatar answered Oct 19 '22 10:10

A5C1D2H2I1M1N2O1R2T1