in Education by
I have the following data frame, I want to find the name of the column with the minimum date for each Id. id a_date b_date c_date d_date 1 2014-01-01 2014-01-05 2014-01-15 NA 2 2017-02-01 NA NA 2015-03-01 Which is: df <- tibble(id = c(1, 2), a_date = c("01/01/14", "01/02/17"), b_date = c("05/01/14", NA), c_date = c("15/01/14", NA), d_date = c(NA, "01/03/15")) I tried this, but did not work df %>% group_by(id) %>% mutate( min = pmin(a_date, b_date, c_date, d_date ,na.rm=TRUE) %>% mutate(col_name = which(colnames(df)== min) Expected output will be as below id col_name 1 a_date 2 d_date JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
How about this? df %>% gather(key, date, -id) %>% filter(!is.na(date)) %>% mutate(date = dmy(date)) %>% group_by(id) %>% arrange(date) %>% slice(1) It produces this output: # A tibble: 2 x 3 # Groups: id [2] id key date 1 1 a_date 2014-01-01 2 2 d_date 2015-03-01

Related questions

0 votes
    Which are indexed by either row or column using a specific name or number? (a) Datasets (b) Data frames (c ... and Out of R Programming Select the correct answer from above options...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    The dplyr package can be installed from CRAN using __________ (a) installall.packages( dplyr ) (b) install. ... of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    The dplyr package can be installed from GitHub using the _______ package. (a) dev (b) devtools (c) ... Operations of R Programming Select the correct answer from above options...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    Which package can be integrated with dplyr for large fast tables? (a) Table (b) Data, dplyr (c) Data. ... Debugging of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    dplyr can be integrated with the ________ package for large fast tables. (a) data.table (b) read.table ... Operations of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    How many functions exist for wrangling the data with dplyr package? (a) one (b) seven (c) three (d) ... Data Analysis of R Programming Select the correct answer from above options...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    My initial dataframe looks: library(tidyverse) df...
asked Apr 23, 2022 in Education by JackTerrance
0 votes
    My initial dataframe looks: library(tidyverse) df...
asked Apr 23, 2022 in Education by JackTerrance
0 votes
    To get the current date, the _______ function will return a Date object which can be converted to a different ... of R Programming Select the correct answer from above options...
asked Feb 16, 2022 in Education by JackTerrance
0 votes
    What is the function to set column names for a matrix? (a) names() (b) colnames() (c) col.names() ... Getting Started of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    Which of the following code will drop the nth column? (a) new...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    Which of the following code drop the ith and jth column? (a) new...
asked Feb 12, 2022 in Education by JackTerrance
0 votes
    Which of the following statement finds the maximum for each column? (a) apply(x,2,max) (b) col.max(x ... Regression of R Programming Select the correct answer from above options...
asked Feb 11, 2022 in Education by JackTerrance
0 votes
    How can I sort a data.frame by multiple columns. E.x. I would like to sort the column q(descending) by column f (ascending) dd...
asked Jan 20, 2022 in Education by JackTerrance
0 votes
    Which of the following specifies the required minimum number of observations for each column pair in order to ... and answers pdf, Data Science interview questions for beginners...
asked Oct 29, 2021 in Education by JackTerrance
...