Quantcast
Channel: Active questions tagged r - Stack Overflow
Viewing all articles
Browse latest Browse all 201839

Adding specific column value according to row value

$
0
0

I have a dataframe H

Genotype Preference
Rice     1
Rice     2
Lr       3
Lr       3
th       4
th       7

I want the dataframe to look like

Genotype Preference Haplotype
Rice     1          1
Rice     2          1
Lr       3          2
Lr       3          2
th       4          0.5
th       7          0.5

That is I want to add a numerical variable to be added to the each type of genotype. I have around 100 observations for each type of genotype. I want to be able to add the numberical variable into a new column in a single line of code and ensure that 1 is added corresponding to rice, 2 to Lr and 0.5 to th.

I tried constructing the code with the mutate/ifelse ` h3 %>% select(Genotype) %>% mutate(Type = ifelse (Genotype = c("Rice"), 1, Genotype)) Other results which I looked up, provide solutions for adding a column with a calculated value from the previous columns but not specific values.

I have found this dplyr mutate with conditional values and Apply R-function to rows depending on value in other column but dont know how to modify it for my code.

Any help with this will be greatly appreciated.


Viewing all articles
Browse latest Browse all 201839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>