Home > other >  How to make a histogram of a non numeric column in R?
How to make a histogram of a non numeric column in R?

Time:10-21

I have a data frame called mydata with multiple columns, one of which is Benefits, which contains information about samples whether they are CB (complete responding), ICB (Intermediate) or NCB (Non-responding at all). So basically the Benefit column is a vector with three values:

Benefit <- c("CB" , "ICB" , "NCB")

I want to make a histogram/barplot based on the number of each one of those. So basically it's not a numeric column. I tried solving this by the following code :

hist(as.numeric(metadata$Benefit))

tried also

barplot(metadata$Benefit)

didn't work obviously. The second thing I want to do is to find a relation between the Age column of the same data frame and the Benefit column, like for example do the younger patients get more benefit ? Is there anyway to do that ? THANKS!

CodePudding user response:

Hi and welcome to the site :)

One nice way to find issues with code is to only run one command at the time.

# lets create some data
metadata <- data.frame(Benefit = c("ICB", "CB", "CB", "NCB"))

now the command 'as.numeric' does not work on character-data

as.numeric(metadata$Benefit) # returns NA's

Instead what we want is to count the number of instances of each unique value of the column Benefit, we do this with 'table'

tabledata <- table(metadata$Benefit)

and then it is the barplot function we want to create the plot

barplot(tabledata)
  • Related