Hey guys
Below you find the R code for my work. I am new with R and I need some help with the package tidyverse and some quite simpel tasks.
1. I would like to create a new table with my last comande and call it. Year_mean
2. I would like to extract my data to excel.
And I cant understand why the specific variabel "Laender Welt --- Laender" doesnt give me any results?
I will atach the csv files if I can do this.
Thank you in advance
Oliver
# packages ----------------------------------------------------------------
library(tidyverse)
rm(list=ls())
# load data ---------------------------------------------------------------
setwd("C:\Users\olive\Desktop\BA Studien\R Daten BA\Daten")
im <- read.csv(file = "import.csv")
ex <- read.csv(file = "export.csv")
# combine -----------------------------------------------------------------
im_ex <- expand_grid(Year = sort(unique(c(im$Year, ex$Year))),
Country = sort(unique(c(im$PartnerName, ex$ReporterName))))
ex <- ex %>%
mutate(Country = ReporterName,
ex = `TradeValue.in.1000.USD`) %>%
select(Year, Country, ex)
im <- im %>%
mutate(Country = PartnerName,
im = `TradeValue.in.1000.USD`) %>%
select(Year, Country, im)
im_ex <- im_ex %>%
left_join(ex) %>%
left_join(im) %>%
mutate(gap = log(1 + im) - log(1 + ex))
im_ex[,3:4][is.na(im_ex[,3:4])] <- 0
im_ex <- im_ex %>%
left_join(ex) %>%
left_join(im) %>%
mutate(gap = log(1 + im) - log(1 + ex))
# plot --------------------------------------------------------------------
im_ex %>%
filter(Country == "Germany") %>%
ggplot(aes(x = Year, y = gap)) +
geom_line()
im_ex %>%
filter(Country == "Germany") %>%
as_tibble()
im_ex %>%
filter(Country == "United Arab Emirates") %>%
ggplot(aes(x = Year, y = gap)) +
geom_line()
im_ex %>%
filter(Country == "United Arab Emirates") %>%
as_tibble()
im_ex %>%
filter(Country == "Afghanistan") %>%
as_tibble()
im_ex %>%
filter(Country == "Laender Welt --- Laender") %>%
as_tibble()
im_ex %>%
filter(Country == "United States") %>%
as_tibble()
im_ex %>%
filter(Country == "United States") %>%
as_tibble()
# Summary Stat ------------------------------------------------------------
## Average Gap per country
im_ex %>%
group_by(Country) %>%
summarise(nr_of_obs = sum(!is.na(gap)),
gap = mean(gap, na.rm = TRUE)) %>%
arrange(gap) %>%
filter(!is.na(gap)) %>%
View()
im_ex %>%
group_by(Year) %>%
summarise(nr_of_obs = sum(!is.na(gap)),
gap = mean(gap, na.rm = TRUE)) %>%
arrange(gap) %>%
filter(!is.na(gap)) %>%
view()
Below you find the R code for my work. I am new with R and I need some help with the package tidyverse and some quite simpel tasks.
1. I would like to create a new table with my last comande and call it. Year_mean
2. I would like to extract my data to excel.
And I cant understand why the specific variabel "Laender Welt --- Laender" doesnt give me any results?
I will atach the csv files if I can do this.
Thank you in advance
Oliver
# packages ----------------------------------------------------------------
library(tidyverse)
rm(list=ls())
# load data ---------------------------------------------------------------
setwd("C:\Users\olive\Desktop\BA Studien\R Daten BA\Daten")
im <- read.csv(file = "import.csv")
ex <- read.csv(file = "export.csv")
# combine -----------------------------------------------------------------
im_ex <- expand_grid(Year = sort(unique(c(im$Year, ex$Year))),
Country = sort(unique(c(im$PartnerName, ex$ReporterName))))
ex <- ex %>%
mutate(Country = ReporterName,
ex = `TradeValue.in.1000.USD`) %>%
select(Year, Country, ex)
im <- im %>%
mutate(Country = PartnerName,
im = `TradeValue.in.1000.USD`) %>%
select(Year, Country, im)
im_ex <- im_ex %>%
left_join(ex) %>%
left_join(im) %>%
mutate(gap = log(1 + im) - log(1 + ex))
im_ex[,3:4][is.na(im_ex[,3:4])] <- 0
im_ex <- im_ex %>%
left_join(ex) %>%
left_join(im) %>%
mutate(gap = log(1 + im) - log(1 + ex))
# plot --------------------------------------------------------------------
im_ex %>%
filter(Country == "Germany") %>%
ggplot(aes(x = Year, y = gap)) +
geom_line()
im_ex %>%
filter(Country == "Germany") %>%
as_tibble()
im_ex %>%
filter(Country == "United Arab Emirates") %>%
ggplot(aes(x = Year, y = gap)) +
geom_line()
im_ex %>%
filter(Country == "United Arab Emirates") %>%
as_tibble()
im_ex %>%
filter(Country == "Afghanistan") %>%
as_tibble()
im_ex %>%
filter(Country == "Laender Welt --- Laender") %>%
as_tibble()
im_ex %>%
filter(Country == "United States") %>%
as_tibble()
im_ex %>%
filter(Country == "United States") %>%
as_tibble()
# Summary Stat ------------------------------------------------------------
## Average Gap per country
im_ex %>%
group_by(Country) %>%
summarise(nr_of_obs = sum(!is.na(gap)),
gap = mean(gap, na.rm = TRUE)) %>%
arrange(gap) %>%
filter(!is.na(gap)) %>%
View()
im_ex %>%
group_by(Year) %>%
summarise(nr_of_obs = sum(!is.na(gap)),
gap = mean(gap, na.rm = TRUE)) %>%
arrange(gap) %>%
filter(!is.na(gap)) %>%
view()
Comment