2

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!

Kev
  • 375
  • 1
  • 7
  • It feels you are trying to do a barplot: use the ``barplot`` function. – Maël Oct 20 '21 at 11:59
  • Didn't work error : need finite 'ylim' values – Kev Oct 20 '21 at 12:04
  • It's better to ask only one question per post. However, since the hist question is answered: An ANOVA can tell you if the (continuous) age distribution between the (discrete) Benefits differ significantly. – dario Oct 20 '21 at 12:04
  • 1
    You could improve your chances of finding help here by adding a [minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610). Adding a MRE and an example of the desired output (in code form, not tables and pictures) makes it much easier for others to find and test an answer to your question. That way you can help others to help you! P.S. Here is [a good overview on how to ask a good question](https://stackoverflow.com/help/how-to-ask) – dario Oct 20 '21 at 12:05
  • Please provide enough code so others can better understand or reproduce the problem. – Community Oct 20 '21 at 12:08
  • This is the only info that I have just a data frame with a bunch of columns.. the barplot function didn't fix it, it's still giving me error. – Kev Oct 20 '21 at 12:14

1 Answers1

0

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)
Jagge
  • 938
  • 4
  • 21