Wwen
Wwen

Reputation: 11

Rbind() doesn't work with character data with different names

I have tried to add a row to an existing dataset which I read into R from a csv file.

The dataset looks like this:

         Format PctShare
1      NewsTalk     12.6
2       Country     12.5
3  AdultContemp      8.2
4        PopHit      5.9
5   ClassicRock      4.7
6    ClassicHit      3.9
7   RhythmicHit      3.7
8    UrbanAdult      3.6
9      HotAdult      3.5
10 UrbanContemp      3.3
11      Mexican      2.9
12    AllSports      2.5

After naming the dataset "share", I tried to add a 13th row to it by using this code:

totalshare <- rbind(share, c("Others", 32.7) 

--> which didn't work and gave me this warning message:

Warning message:In`[<-.factor`(`*tmp*`, ri, value = "Others"):invalid factor level, NA generated

However, when I tried entering a row with an existing character value ("AllSports") in the dataset with this code:

rbind(share, c("AllSports", 32.7)) 

--> it added the row perfectly

I am wondering whether I need to tell R that there is a new character value under the column "Format" before I bind the new row to R?

Upvotes: 0

Views: 1271

Answers (2)

JasonAizkalns
JasonAizkalns

Reputation: 20473

Your format columns is a factor variable. Look at str(share), str(share$format), class(share$format) and levels(share$format) for more information. The reason rbind(share, c("AllSports", 32.7) worked is because "AllSports" is already an existing factor level for the format variable.

To fix the issue, convert the format column to character via:

share$format <- as.character(share$format)

Do some searches on factor variables and setting factor levels to learn more. Moreover, when you are reading in the file from csv, you can force any character strings to not convert to factors with the option, stringsAsFactors = FALSE -- for example, share <- read.csv(myfile.csv, stringsAsFactors = FALSE).

Upvotes: 2

Indranil Gayen
Indranil Gayen

Reputation: 710

Two solution I have in mind

Solution 1:- before reading data

options(stringsAsFactors = F)

or Solution 2:- as suggested by @JasonAizkalns

Upvotes: 1

Related Questions