1

Say, I have a dataframe with three columns:

Year     Sales     Income
1         100       30
2         200       20
3         NA        10
4         300       50
5         NA       -20

I want to get all the 'Year' that has a particular value in 'Sales', ignoring other columns. For example, if I ask for NA, I should get:

Year    Sales
3       NA
5       NA

Please note there is no Income in the above data frame.

3 Answers 3

1

We can use base R with subset

subset(df, is.na(Sales), select = c('Year', 'Sales'))
#  Year Sales
#3    3    NA
#5    5    NA

data

df <-structure(list(Year = 1:5, Sales = c(100L, 200L, NA, 300L, NA
), Income = c(30L, 20L, 10L, 50L, -20L)), class = "data.frame", 
 row.names = c(NA, -5L))
Sign up to request clarification or add additional context in comments.

Comments

0

You can try with base R. In a dataframe you can index by rows (left to ,) and by columns (right to ,) inside the brackets. So, you can specify the conditions, in this case NA in Sales and then select the variables like Year and Sales. Here the code:

#Code
df[is.na(df$Sales),c('Year','Sales')]

Output:

  Year Sales
3    3    NA

Some data used:

#Data
df <- structure(list(Year = 1:4, Sales = c(100L, 200L, NA, 300L), Income = c(30L, 
20L, 10L, 50L)), class = "data.frame", row.names = c(NA, -4L))

Comments

0

you can use the %in% subsetting which has to be declared before. I usually use these in ggplot directly. hope they work independently too. For example:

col11 <- c("c11", "c12")
typecol2 <- c("c22", "c24")
data_new <- subset(data old, (col1 %in% col11) & (col2 %in% typecol2))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.