1

I have a proposal that foreign policy has less vetos and overrides than domestic policy and have collated data from congress.gov. I have two separate csv files one for foreign and one for domestic. I need a way to give both a variable for being either domestic and foreign easily as I have 1500 results all-together. And then merge the two probably in R. Also both files have identical columns currently.

This is what I have tried:

data1 <- read.csv("(DP.csv")
data2 <- read.csv("(FP.csv")

newdata = merge(data1, data2)

But this returns 242 obs. of previously 1156 obs. (data1) and 509 obs. (data2).

Phil
  • 7,287
  • 3
  • 36
  • 66
randomname
  • 11
  • 3
  • Hi could you provide us a small script you tried or at least a minimum example to help you ? See this : https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example/5963610#5963610 – Gowachin Apr 12 '21 at 12:41
  • sure thanks I did so, data1 <- read.csv("(DP.csv") data2 <- read.csv("(FP.csv") newdata = merge(data1, data2) – randomname Apr 12 '21 at 13:02
  • Well, please read the link I gave before, we need to see what your csv file look like. Could you provide us small tables that could represent a part of your data ? – Gowachin Apr 12 '21 at 13:18
  • You should paste and show us some of the content of the CSV files. We need to know at least the column names to understand why you are obtaining less rows. – crestor Apr 12 '21 at 19:20

2 Answers2

1

Try this:

library(tidyverse)

data1 <- read_csv("DP.csv")
data2 <- read_csv("FP.csv")

newdata <- data1 %>% full_join(data2)

Or:

data1 <- read.csv("DP.csv") 
data2 <- read.csv("FP.csv")

newdata <- data1 = merge(data1, data2)
TarJae
  • 72,363
  • 6
  • 19
  • 66
0

Probably, what you need is not merge. You need to bind them:

library(tidyverse)

data1 <- read_csv("DP.csv") %>%
  mutate(policy = "domestic")
data2 <- read_csv("FP.csv") %>%
  mutate(policy = "foreign")
data <- bind_rows(data1, data2)
crestor
  • 1,388
  • 8
  • 21