Values and indices greater than... in one dataframe

Hi,
I was curious whilst learning and reading these posts:
https://community.rstudio.com/t/return-the-first-position/90407/7

and this post:
https://community.rstudio.com/t/help-deleting-value-in-a-list-greater-than-10/52113/4

and I started with this:

x <- data.frame(x = c(2, 5, 4, 4, 5, 5, 5, 5))
 which(x > 4)

So it gives me an indices of values that are greater than 4.
I would like to have a data frame that first column is like in original x data frame:

View(x)

obraz

and in next created column that will be indices of values that are greater than 4, and then in next column will be a column with real values greater than 4. All other cells will be filled with NA.
So I tried:

greater_than_4<-lapply(x,function(x)which(x > 4))

that gives me a list with indices of values, that are greater than 4 (but maybe purrr would be better than lapply), and this:

which(x > 4, arr.ind = TRUE) %>% as.data.frame()

# or:
vector_in_one_column <- as.data.frame(t(greater_than_4))

gives me a data frame with indices.

And next command gives me values, not indices:

x[x>4]

I would like to ask for advice how to glue it together into one dataframe ?
Thanks

Is this what you're trying to do?

library(dplyr, warn.conflicts = FALSE)

x <- data.frame(x = c(2, 5, 4, 4, 5, 5, 5, 5))

x %>% 
  mutate(y = if_else(x > 4, row_number(), NA_integer_),
         z = if_else(x > 4, x, NA_real_))
#>   x  y  z
#> 1 2 NA NA
#> 2 5  2  5
#> 3 4 NA NA
#> 4 4 NA NA
#> 5 5  5  5
#> 6 5  6  5
#> 7 5  7  5
#> 8 5  8  5

Created on 2020-12-13 by the reprex package (v0.3.0)

Yes, thank you, your solution is short and elegant.
I tried different and a few more approaches like:

x <- data.frame(x = c(2, 5, 4, 4, 5, 5, 5, 5))

greater_than_4<-lapply(x,function(x)which(x > 4))
vector_in_one_column <- as.data.frame(t(greater_than_4))

x[x, which(x > 4)]
as.data.frame(t(greater_than_4))
x <- rownames_to_column(x, "Index")
x$generated_uid <- 1:nrow(x)
x$where_is_fullfilled_condition <- ifelse( x$x >4, x$Index , NA)

x3 <- x %>% dplyr::select(generated_uid, everything()) 

and I accomplished that:

But I have learnt row_number() and NA_real_ from you, thank you.
best regards.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.