0

I'm new to R and couldn't find a solution to this. I have a data set with Country Codes, Values, and Years(Panel Data) The 'Value' column has many NAs. I would like to, for each country, get a list of years for which the values are NA. Would this be possible using the dplyr function? This is a snapshot of my data set Country codes, Years and Values

Skurup
  • 205
  • 3
  • 10
  • When asking for help, you should include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Jun 15 '18 at 18:59
  • Point noted. Thanks. – Skurup Jun 15 '18 at 19:29
  • 1
    Please post data in the post as text, preferably using `dput`, not as an image – camille Jun 15 '18 at 20:18

3 Answers3

1

Do you mean like this?

DAT = read.table(text="Country.Code  Year  Value
UKR            2006  NA
UKR           2007  NA
UKR           2008  2000
ARE           2006   NA
ARE           2007   NA",
header=TRUE)

DAT[is.na(DAT$Value), 1:2]
  Country.Code Year
1          UKR 2006
2          UKR 2007
4          ARE 2006
5          ARE 2007

Addition

To get all years for one country in a single line, you could use

temp = DAT[is.na(DAT$Value), 1:2]
aggregate(temp$Year, list(temp$Country.Code), paste, collapse=",")
  Group.1         x
1     ARE 2006,2007
2     UKR 2006,2007
G5W
  • 36,531
  • 10
  • 47
  • 80
1

Use the which function:

df[is.na(which(df$value)),]

Rishi
  • 313
  • 1
  • 4
  • 18
1

Making the test case:

df <- read.table(text="Country  Year Value
UKR            2006  NA
UKR           2007  NA
UKR           2008  2000
ARE           2006   NA
ARE           2007   NA", header=TRUE)

for each country, get a list of years for which the values are NA

lapply(split(df, df["Country"]), function(x) x$Year[is.na(x$Value)])
# or equivalent but more readable
with(subset(df, is.na(Value)), split(Year, Country))

Output:

$ARE
[1] 2006 2007

$UKR
[1] 2006 2007

Is this what you need?

lebatsnok
  • 6,329
  • 2
  • 21
  • 22