HertieDataScience / SyllabusAndLectures

Hertie School of Governance Introduction to Collaborative Social Science Data Analysis
MIT License
37 stars 60 forks source link

Recode character into numeric not working #66

Closed mr-r0b0t closed 8 years ago

mr-r0b0t commented 8 years ago

Hi everyone,

Another issue I wasn't able to fix yet: I would like to recode a variable that is a character into factor in order to create a histogram. Unfortunately, recoding with "as.numeric()" doesn't work (same as some other things I have tried), probably because of some missing values or two observations with a string (rows 98 and 365).

Has anyone encountered a similar problem or knows how to fix this? Here's the code for reproduction of the issue.

Load relevant dataset

Model_1_Dataset <- read.csv("https://raw.githubusercontent.com/KatrinHeger/CollaborativeResearchProject/master/Model_1_Dataset.csv")

Recode into numeric

as.numeric(Model_1_Dataset$rtypesup_cat)

is.numeric(Model_1_Dataset$rtypesup_cat) # already indicates that it didn't work

Create the histogram

hist(Model_1_Dataset$rtypesup_cat, main = "Frequency of Rebel Support Types", xlab = "Rebel Support Types", las = 1, breaks = seq(.5,3.5,1), col = "lightyellow")

Thank's a lot!

Best,

Benedikt

mr-r0b0t commented 8 years ago

We could resolve it by using the qplot function of the ggplot2 package.