Ave-command i R statistics

Imaging you have some data on unemployment:

unemployment.rate <- c(0.01, 0.17, 0.19, NA, 0.21, 0.14, 0.02,NA, 0.26, 0.27, 0.21, 0.28, 0.23, 0.16, 0.1, NA, 0.23, 0.03, 0.11)
cntry <- c ("SE", "NO", "DK", "SE", "NO", "SE", "DK", "DK", "NO", "DK", "SE", "DK", "DK", "SE", "DK", "SE", "SE", "DK", "NO")
size <- c("Big","Medium","Big","Big","Medium","Small","Big","Medium","Medium","Big","Small","Medium","Medium","Big","Medium","Big","Big","Big","Small")
df <- data.frame(unemployment_rate, cntry, size)

In the data we have some NA. We want to replace these based on the mean of cntry. This can be done using the ave-command (from R-base):

df$unemployment_rate <- ave(df$unemployment.rate, list(df$cntry), FUN=function(x) {x[is.na(x)] <- mean(x,na.rm=TRUE); x; })

We can actually base the new data on both the mean fron cntry and the mean from size.

df$unemployment_rate <- ave(df$unemployment.rate, list(df$cntry, df$size), FUN=function(x) {x[is.na(x)] <- mean(x,na.rm=TRUE); x; })

Amazing! (edited)

How to get R up and running in Fedora Linux

It is trivial to install R in Fedora. Just type:

sudo dnf install R

After that you can run R in the terminal. If you want an environment to work in you can use RStudio. I use Emacs and ESS. Everything works nice up until the point you want to install a package — say for example car. It wont work (out of the box). You need to install an additional program. In the terminal:

sudo dnf install libcurl-devel 

libcurl is a client-side URL transfer library that you need to install packages in R.

It is also a good idea to install:

sudo dnf install NLopt

NLopt is a library for nonlinear optimization, callable from R.