it is usually better if you provide a data table in a way that lets u quickly generate it ourselves, in your case that would be:
df <- data.frame(
us_gdp = c(1,2,3),
uk_gdp = c(NA,1,2),
jp_gdp = c(NA,NA,2),
us_inf = c(2,2,2),
uk_inf = c(3,1,2),
jp_inf = c(NA,2,4),
us_bond = c(3,4,1),
uk_bond = c(3,1,1),
jp_bond = c(2,1,1),
row.names = c("Jan-78","Feb-78","Mar-78")
)
not sure exactly what output you want, but I came up with this:
- make a vector for each country, just stating the column-numbers for each of its entries (e.g. for us that would be: c(1,4,7). It is easiest to use the seq() function. Make sure to set the by-value to the number of countries you have in total, the to-value to the total number of columns you have, and the from-value is individual for each country.
us <- seq(from=1, to=9, by=3)
uk <- seq(from=2, to=9, by=3)
jp <- seq(from=3, to=9, by=3)
- isolate all columns for a certain country from the data.frame, then use the na.omit() function to throw out any rows that contain an NA in any column
df_us <- na.omit(df[,us])
df_uk <- na.omit(df[,uk])
df_jp <- na.omit(df[,jp])
this will give you a data.frame for each country, containing all columns for that country, but only the rows where there is no NA.