dimanche 5 mai 2019

R plot kernel densities for subsets of a dataset (conditional on char variable)

I want to plot kernel densities for specific subsets of my dataset. The subsets are to be identified by a char variable. My dataset has the following structure (not my actual dataset but the general structure):

Char_var    var1   var2  var3  k_var
Material A                      2
Material B                      5
Material C                      7
Material A                      8
Material C                      4
.                               .
.                               .
.                               .

var1, var2, var3 are other doubles but not necessary for this plot.

Generally, I coded it so far like this

dens1 <-  density(k_var) # How do I add an if statement for the Char_var here?
plot(dens1)

If I do it this way, I would need to write the code above for every material in my dataset. Is there a more elegant way to code that such that I get the density plots for every material or do I need to split it up for every material as I intended to do? ...as I have acutally more than three materials in my dataset. Thanks!

Aucun commentaire:

Enregistrer un commentaire