melunuge92 melunuge92 -4 years ago 77
R Question

Logical order in R to create column in data frame and fill it with information

I've got a df with the following column among others (with superpopulations ID). it's colname is Superpop:

EUR
EUR
EUR
AMR
AMR
AFR
AMR
AFR
EUR
SAS
EUR
...


and I need (for later using scatterplot3d) to build another column, let's say the column named ¨pcolors¨, which has to have color names to identify the superpopulations to color the points in the plot. I want this as an output:

EUR red
EUR red
EUR red
AMR blue
AMR blue
AFR green
AMR blue
AFR green
EUR red
SAS yellow
EUR red
... ...


the thing is that they aren't sorted, and the df is 2524 lines long, so i can't do it manually and i would prefer not to sort it because of the order of the other columns. Is there a way, for instance, with a logical function to say ¨generate another column, and , if in that line Superpop==EUR, then write ¨red¨ at that line in pcolor column...¨ and so on for the 5 superpopulations i've got ? Any thoughts? Thanks!

Answer Source

This is pretty simple:

pcolors <- unsplit(superpop, value = colors())

You can pick the colors you want in the value-parameter.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download