For a sample dataframe:
df1 <- structure(list(id = structure(1:5, .Label = c("a", "b", "c",
"d", "e"), class = "factor"), cat = c(5L, 7L, 6L, 2L, 8L), dog = c(7L,
NA, 6L, 13L, 2L), sheep = c(NA, 6L, 3L, 6L, 2L), cow = c(2L,
10L, 8L, 9L, 1L), rabbit = c(5L, 3L, NA, 2L, 4L), pig = c(7L,
NA, 12L, 5L, NA)), .Names = c("id", "cat", "dog", "sheep", "cow",
"rabbit", "pig"), class = "data.frame", row.names = c(NA, -5L
))
I want to add an extra column 'complete.farm' to identify which rows have values in columns 'sheep' AND 'cow' AND 'pig'. Any rows with NAs in one or more of these columns should get a 0 and rows with real values should get a 1.
If anyone could give me some advice on this, I would really appreciate it. I usually use complete cases to subset my dataframe, but this time, I only want to add this information in a column.