mercredi 4 novembre 2020

Creating a new column with a specific value

I have this data frame (bonds) to which I want to add a column "helix" with values H1, H2, H3, H4, H5, and H6 that are dependent on the value of the column "Number2" as follows:

H1: 789 - 798 H2: 802 - 810 H3: 814 - 824 H4: 828 - 844 H5: 849 - 859 H6: 863 - 873

So far I tried

bonds$Helix1 <- ifelse(bonds$Number2<=798, "YES", "NO")

but this would produce 5 different columns for H1, H2, H3, H4, and H5 with values yes or no, but I want a single column that attributes the values H1, H2, H3, H4, or H5.

I also tried

bonds$Helix <- if(bonds$Number2 <= 798) {H1} && if(bonds$Number2>=802 & bonds$Number2<=810) {H2} && if (bonds$Number2>= 814 & bonds$Number2 <=824) {H3} && if(bonds$Number2>=828 & bonds$Number2<=844) {H4} && if (bonds$Number2>=849 & bonds$Number2 <= 859) {H5} && if(bonds$Number2>=863 & bonds$Number2 <=873) {H6}

but get the error message the condition has length > 1 and only the first element will be used while it's not creating a new column.

Aucun commentaire:

Enregistrer un commentaire