I have data that look like this
bankname bankid year deposit dep_cert capital surplus
Bank A 1 1881 244789 7250 20218 29513
Bank B 2 1881 195755 10243 185151 NA
Bank C 3 1881 107736 13357 177612 NA
Bank D 4 1881 170600 NA 20000 NA
Bank E 5 1881 320000 351266 314012 NA
This is the code to replicate the data.
bankname <- c("Bank A","Bank B","Bank C","Bank D","Bank E")
bankid <- c( 1, 2, 3, 4, 5)
year<- c( 1881, 1881, 1881, 1881, 1881)
deposit <- c(244789, 195755, 107736, 170600, 32000000)
dep_cert<-c(7250,10243,13357,NA,351266)
capital<-c(20218,185151,177612,20000,314012)
surplus<-c(29513,NA,NA,NA,NA)
bankdata<-data.frame(bankname, bankid,year,deposit, dep_cert, capital, surplus)
I want to create a new column called liability
as the sum of deposit
, dep_cert
, capital
, and surplus
. This means that the data would look like this.
bankname bankid year deposit dep_cert capital surplus liability
Bank A 1 1881 244789 7250 20218 29513 301770
Bank B 2 1881 195755 10243 185151 NA 391149
Bank C 3 1881 107736 13357 177612 NA 298705
Bank D 4 1881 170600 NA 20000 NA 190600
Bank E 5 1881 320000 351266 314012 NA 32665278
However, when I used the sum
command in R, I got NAs due to missing values. In Stata, I would do
egen liability = rowtotal(deposit, dep_cert,capital, surplus)
What would be the equivalent code in R?
Also, my second question is, to replace all the missing values (NAs) with the number 0 in the data, in Stata, I would do
foreach x of varlist deposit dep_cert capital surplus {
replace `x'=0 if missing(`x')
}
What would be the equivalent code in R?