Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
x = c(1,2,3)
class(x)
x = F
class(x)
d = read.csv("data/income_topdecile.csv")
class(d$Year)
d$postwar = d$Year > 1945
head(d)
d$Year = d$Year > 1945
d = d>1945
class(d)
class(d$postwar)
d$Year
var ="Year"
d$var
d[[var]]
head(d)
for (col in colnames(d)[-1]) {
d[[col]][is.na(d[[col]])] = mean(d[[col]], na.rm=T)
}
for (col in colnames(d)[-1]) {
d[is.na(d[[col]]), col] = mean(d[[col]], na.rm=T)
}
d["France"]
d[["France"]]
d$France
y = c(1,2,3)
?c
subset = d[c("Germany", "France")]
head(subset)
d[, c("Germany", "France")]
x = d["France"]
class(x)
mean(x$France)
mean(x)
x = d[,"France"]
class(x)
col = "France"
d[[col]]
head(d)
col = colnames(d)[2]
print(col)
col = colnames(d)[3]
print(col)
col = colnames(d)[4]
print(col)
d$France[is.na(d$France)] = mean(d$France, na.rm=T)
d$coder = "Pietje"
head(d)
m = as.matrix(d)
class(m)
head(m)
d2 = as.data.frame(m)
head(d2)
class(d2$Year)
d2$Year = as.numeric(as.character(d2$Year))
d = read.csv("/tmp/bla.csv")
head(d)
class(d$coder)
levels(d$coder)
as.numeric(d$coder)
table(d$coder)
d = d[d$id > 10, ]
table(d$coder)
d$coder = droplevels(d$coder)
levels(d$coder)
d$coder[d$id == 56] = "piet"
d
d = read.csv("/tmp/bla.csv", stringsAsFactors = F)
class(d$coder)
d = data.frame(coder=c("a", "b", "a"))
d$coder = as.character(d$coder)
head(d)
class(d$coder)
## renaming
d = read.csv("data/income_topdecile.csv")
head(d)
colnames(d) = c("jaar", "vs", "vk", "de", "fr", "zw", "eu")
d = read.csv("data/income_topdecile.csv")
colnames(d)[1] = "jaar"
head(d)
colnames(d)[3] = "UK"
head(d)
d = read.csv("data/income_topdecile.csv")
library(plyr)
d = rename(d, c("U.K."="UK", "U.S."="US"))
head(d)
d = read.csv("data/income_topdecile.csv")
colnames(d)[2:4] = c("US", "UK","DE")
colnames(d)[c(2,3,4)] = c("US", "UK","DE")
head(d)
d = read.csv("data/income_topdecile.csv")
colnames(d)[-1] = c("US", "UK","DE", "FR", "SW", "EU")
head(d)
d = read.csv("data/income_topdecile.csv")
colnames(d)[2:7] = paste("country", 1:6, sep = "")
head(d)
d = read.csv("data/income_topdecile.csv")
d = na.omit(d)
head(d)
d$period = "pre-war"
d$period[d$Year > 1945] = "post-war"
d$period2 = ifelse(d$Year > 1945, "post" ,"pre")
library(car)
d$period3 = recode(d$Year, "lo:1945='pre'; 1945:1980='mod'; else='post'")
d
d$period4 = cut(d$Year, c(1890, 1950, 1980, 2050), c("pre", "rec", "mod"))
class(d$Year)
d = read.csv("/tmp/bla.csv")
d
class(d$time)
d$time2 = as.numeric(as.character(d$time))
d$time = as.character(d$time)
d$time = sub(",", ".", d$time)
d$time = as.numeric(d$time)
class(d$time)
d$time[d$time == 999] = NA
d
d = read.csv("/tmp/bla.csv")
d$time = gsub(",", ".", d$time)
d$time = gsub("[^0-9.]", "", d$time)
d
d = read.csv("/tmp/bla.csv")
d[grepl(",", d$time), ]
sum(grepl(",", d$time))
table(d$coder[grepl("[^0-9.]", d$time)])
d[grepl("jan", d$coder, ignore.case = T),]
d = read.csv("data/income_toppercentile.csv")
head(d)
e = arrange(d, -Denmark, Spain)
e
d = d[order(d$Denmark), ]
head(d)
d = read.csv("/tmp/bla.csv")
d$time2 = gsub(",", ".", d$time)
d$time2 = gsub("[^0-9.]", "", d$time2)
numbers = c("one", "two", "three", "four", "five")
for (n in 1:5) {
d$getal[grepl(numbers[n], d$time)] = n
}
d$getal[grepl("half", d$time)] = d$getal[grepl("half", d$time)] + 0.5
d$time2[!is.na(d$getal)] = d$getal[!is.na(d$getal)]
d
x = 3
x <- 3
3 -> x
d = read.csv("data/income_topdecile.csv")
d2 = d[d$Year <= 1940,]
d3 = d[d$Year >= 1940,]
d2
d3$France = NULL
head(d3)
d = read.csv("data/income_topdecile.csv")
d2 = d[1:3]
d3 = d[c(1, 4:6)]
d3 = arrange(d3, -Year)
d3 = d3[d3$Year > 1945,]
head(d3)
d = cbind(d2, d3)
d = merge(d2, d3)
head(d)
d = merge(d2, d3, by = "Year")
head(d)
colnames(d3)[1] = "Jaar"
head(d2)
head(d3)
d = merge(d2, d3, by.x = "Year", by.y="Jaar")
head(d)
resp = read.csv("/tmp/resp.csv")
meas = read.csv("/tmp/meas.csv")
merge(resp, meas, all.x=T)
d = read.csv("data/income_topdecile.csv")
d = na.omit(d)
d2 = d[c("Year", "U.S.")]
d3 = d[c(1, ncol(d))]
merge(d2, d3)
library(reshape2)
long = melt(d, id.vars = "Year")
View(long)
long$value[long$value > .4] = .4
dcast(long, Year ~ variable)
head(long)
long =rename(long, c("variable"="country"))
long$period = cut(long$Year, c(1800, 1920, 1960, 1980, 2050), c("a","b","c","d"))
dcast(long, period ~ country, fun.aggregate = mean)
long
public = read.csv("data/public_capital.csv")
private = read.csv("data/private_capital.csv")
x = merge(public, private, by="Year")
head(x)
?melt
public = melt(public, id.vars = "Year")
private = melt(private, id.vars = "Year")
colnames(public)[-1] = c("country", "public")
colnames(private)[-1] = c("country", "private")
head(public)
head(private)
wealth = merge(public, private)
cor.test(wealth$public, wealth$private)
wealth$total = wealth$public + wealth$private
head(wealth)(
dcast(wealth, Year ~ country, value.var = "total")
dcast(wealth, country ~ ., value.var = "total", fun.aggregate = mean, na.rm=T)
dcast(wealth, . ~ country, value.var = "total", fun.aggregate = mean, na.rm=T)
wealth$ctype = "continental"
wealth$ctype[wealth$country %in% c("U.S." ,"U.K", "Canada")] = "anglo"
table(wealth$ctype)
dcast(wealth, Year ~ ctype, value.var = "total", fun.aggregate = mean, na.rm=T)
#####
aggregate(wealth[c("private", "public", "total")], wealth[c("Year", "ctype")], mean, na.rm=T)
aggregate(wealth[wealth$ctype == "anglo", c("private", "public", "total")], wealth[wealth$ctype == "anglo", c("Year"), drop=F], mean, na.rm=T)
anglo = wealth[wealth$ctype == "anglo",]
aggregate(anglo[c("private", "public", "total")], anglo["Year"], mean, na.rm=T)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment