Reputation: 107
I'm trying to create a vector using data from my data frame which contains all of the numeric values in the data frame.
Basically, I want a vector that has (2,2,5,2,2,3,2,3,2,2,2,2,2)
.
two three four five six seven
2 NA NA NA NA NA
2 NA NA NA NA NA
NA NA NA 5 NA NA
2 NA NA NA NA NA
2 NA NA NA NA NA
NA 3 NA NA NA NA
2 NA NA NA NA NA
NA 3 NA NA NA NA
2 NA NA NA NA NA
2 NA NA NA NA NA
2 NA NA NA NA NA
2 NA NA NA NA NA
2 NA NA NA NA NA
Upvotes: 1
Views: 85
Reputation: 20463
Just subset the dataframe for non-NA values with !is.na(df)
:
df <- data.frame(two = c(2, 2, NA),
three = c(NA, NA, NA),
four = c(NA, 3, NA))
df
# two three four
# 1 2 NA NA
# 2 2 NA 3
# 3 NA NA NA
is.na(df)
# two three four
# [1,] FALSE TRUE TRUE
# [2,] FALSE TRUE FALSE
# [3,] TRUE TRUE TRUE
df[!is.na(df)]
# [1] 2 2 3
Upvotes: 1