0

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?

Parfait
  • 104,375
  • 17
  • 94
  • 125
  • Can you just convert the column from factor to numeric first? As in here: https://stackoverflow.com/questions/27528907/how-to-convert-data-frame-column-from-factor-to-numeric – Joe Mar 14 '22 at 00:16
  • US Senate or US Supreme Court? Does `confirm_margin` have *any* data prior to this attempt? – Parfait Mar 14 '22 at 03:25
  • 1
    Please create a reproducible example showing your input data, desired output data, and the code you have tried so far. That will allow people to help you – psychonomics Mar 14 '22 at 11:55

1 Answers1

0

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
Simon.S.A.
  • 6,240
  • 7
  • 22
  • 41