'How to deal with a factor that's a proportion in R?
I am dealing with a dataset that has confirmation margins for individuals confirmed in the US Senate. The dataset I'm pulling from has them coded as factors with each possible margin (100/0, 50/50, etc) as a different level. I need to assign the values of these margins to a column in another data frame. Right now my code looks something like:
for (i in fedjud_scotus$Judge.Name) {
justice_data$confirm_margin[justice_data$justice==i] <- fedjud_scotus$Ayes.Nays[fedjud_scotus$Judge.Name==i]
}
where fedjud_scotus is the original data frame, and justice_data is the new data frame I'm trying to add confirmation data into. Right now, this is only moving the level (ex. 3,4,8), not the actual margin (64/36, 93/7, etc). Is there a way to get the actual margin data to move where I want it?
Solution 1:[1]
Without a reproducible example, it is difficult to know exactly what you are looking for. I have taken a guess below.
Once you convert the factors to strings, you can do this using string manipulation. The strsplit function will split a string into parts. However, it does not play nicely with dplyr.
However, this question and its answers provide multiple options for approaches that use the same idea but implemented in a way that works nicely with dplyr.
Example solution:
library(dplyr)
library(tidyr)
df = data.frame(proportions = c("100/0","70/30","50/50"),
stringsAsFactors = TRUE)
split = df %>%
mutate(proportions = as.character(proportions)) %>%
separate(proportions, c("win", "loss"), "/")
output = cbind(df, split) %>%
mutate(win = as.numeric(win),
loss = as.numeric(loss))
Gives:
proportions win loss
1 100/0 100 0
2 70/30 70 30
3 50/50 50 50
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Simon.S.A. |
