tidyr

How to pivoting dataframe consisting column with section and sub section In R

放肆的年华 提交于 2020-05-10 04:10:08
问题 I have a below-mentioned dataframe: structure( list(ID = c("P-1", " P-1", "P-1", "P-2", "P-3", "P-4", "P-5", "P-6", "P-7", "P-8"), Date = c("2020-03-16 12:11:33", "2020-03-16 13:16:04", "2020-03-16 06:13:55", "2020-03-16 10:03:43", "2020-03-16 12:37:09", "2020-03-16 06:40:24", "2020-03-16 09:46:45", "2020-03-16 12:07:44", "2020-03-16 14:09:51", "2020-03-16 09:19:23"), Status = c("SA", "SA", "SA", "RE", "RE", "RE", "RE", "XA", "XA", "XA"), Flag = c("L", "L", "L", NA, "K", "J", NA, NA, "H", "G"

remove duplicates and collapse near duplicates based on time difference

你离开我真会死。 提交于 2020-05-08 05:33:38
问题 I have a data-frame like as shown below DF = structure(list(Age_visit = c(48, 48, 48, 49, 49, 77), Date_1 = c("8/6/2169 9:40", "8/6/2169 9:40", "8/6/2169 9:41", "8/6/2169 9:42", "24/7/2169 8:31", "12/9/2169 10:30", "19/6/2237 12:15"), Date_2 = c("NA-NA-NA NA:NA:NA", "NA-NA-NA NA:NA:NA", "NA-NA-NA NA:NA:NA", "NA-NA-NA NA:NA:NA", "NA-NA-NA NA:NA:NA", "NA-NA-NA NA:NA:NA", "NA-NA-NA NA:NA:NA"), person_id = c("21", "21", "21", "21", "21", "21", "31" ), enc_id = c("A21BC","A21BC", "A22BC", "A23BC",

remove duplicates and collapse near duplicates based on time difference

ε祈祈猫儿з 提交于 2020-05-08 05:30:12
问题 I have a data-frame like as shown below DF = structure(list(Age_visit = c(48, 48, 48, 49, 49, 77), Date_1 = c("8/6/2169 9:40", "8/6/2169 9:40", "8/6/2169 9:41", "8/6/2169 9:42", "24/7/2169 8:31", "12/9/2169 10:30", "19/6/2237 12:15"), Date_2 = c("NA-NA-NA NA:NA:NA", "NA-NA-NA NA:NA:NA", "NA-NA-NA NA:NA:NA", "NA-NA-NA NA:NA:NA", "NA-NA-NA NA:NA:NA", "NA-NA-NA NA:NA:NA", "NA-NA-NA NA:NA:NA"), person_id = c("21", "21", "21", "21", "21", "21", "31" ), enc_id = c("A21BC","A21BC", "A22BC", "A23BC",

pivot longer with multiple columns and values

折月煮酒 提交于 2020-04-30 06:25:28
问题 I have a data frame of the format # A tibble: 6 x 8 type id age2000 age2001 age2002 bool2000 bool2001 bool2002 <chr> <chr> <chr> <chr> <chr> <chr> <chr> <chr> 1 1 1 20 21 22 1 1 1 2 1 2 35 36 37 2 2 2 3 1 3 24 25 26 1 1 1 4 2 1 32 33 34 2 2 2 5 2 2 66 67 68 2 2 2 6 2 3 14 15 16 1 1 1 and would like to use pivot_longer from the tidyr universe to generate longitudinal data of the form: # A tibble: 18 x 5 type id age bool year <chr> <chr> <chr> <chr> <chr> 1 1 1 20 1 2000 2 1 1 21 1 2001 3 1 1

grouping table by multiple factors and spreading it from long format to wide - the data.table way in R

感情迁移 提交于 2020-03-25 13:41:36
问题 As an example i will be using the mtcars data available in R: data(mtcars) setDT(mtcars) Lets day I want to group the data by three variables, namely: carb , cyl , and gear . I have done this as follow. However, i am sure there is a better way, as this is quite repetitive. newDTcars <- mtcars [, mtcars[, mtcars[, .N , by = carb], by = cyl], by= gear] Secondly, I would like to have the data in a wide format, where there is a separate column for every gear level. For illustration purpose I have

pivot_longer with multiple classes causes error (“No common type”)

夙愿已清 提交于 2020-03-21 17:41:40
问题 I am running pivot_longer on multiple columns (i.e. two character columns and one numeric). I am encountering an error related to the class mismatch. I have investigated the documentation for any "force" options and did not see any arguments within pivot_longer to specify the class to use -- or to allow the function auto-detect the most general class. Are there any parameters within pivot_longer to avoid this error? Or do you need to convert the columns to a single class before running pivot

pivot_longer with multiple classes causes error (“No common type”)

元气小坏坏 提交于 2020-03-21 17:40:23
问题 I am running pivot_longer on multiple columns (i.e. two character columns and one numeric). I am encountering an error related to the class mismatch. I have investigated the documentation for any "force" options and did not see any arguments within pivot_longer to specify the class to use -- or to allow the function auto-detect the most general class. Are there any parameters within pivot_longer to avoid this error? Or do you need to convert the columns to a single class before running pivot

convert invalid date to a valid date like value using R

房东的猫 提交于 2020-03-16 08:10:52
问题 I have a dataframe like as shown below d1 <- c('21/01/2052 9:02','12/30/2042 9:25','10134-5-28 0:0:0','10109-12-27 0:0:0','10176-6-7 0:0:0') d2 <- c('22/03/2052 9:22','31/10/2043 9:25','10134-6-02 0:0:0','10109-12-30 0:0:0','10176-6-9 0:0:0') df_test <- c(d1,d2) Input I tried something like below using lubridate package but doesn't seem to help mdy_hm(df_test$d1) This resulted in a warning message and resulted in NAs for invalid values It's fine if invalid dates can't be processed and returns

convert invalid date to a valid date like value using R

依然范特西╮ 提交于 2020-03-16 08:10:20
问题 I have a dataframe like as shown below d1 <- c('21/01/2052 9:02','12/30/2042 9:25','10134-5-28 0:0:0','10109-12-27 0:0:0','10176-6-7 0:0:0') d2 <- c('22/03/2052 9:22','31/10/2043 9:25','10134-6-02 0:0:0','10109-12-30 0:0:0','10176-6-9 0:0:0') df_test <- c(d1,d2) Input I tried something like below using lubridate package but doesn't seem to help mdy_hm(df_test$d1) This resulted in a warning message and resulted in NAs for invalid values It's fine if invalid dates can't be processed and returns

convert invalid date to a valid date like value using R

北战南征 提交于 2020-03-16 08:09:31
问题 I have a dataframe like as shown below d1 <- c('21/01/2052 9:02','12/30/2042 9:25','10134-5-28 0:0:0','10109-12-27 0:0:0','10176-6-7 0:0:0') d2 <- c('22/03/2052 9:22','31/10/2043 9:25','10134-6-02 0:0:0','10109-12-30 0:0:0','10176-6-9 0:0:0') df_test <- c(d1,d2) Input I tried something like below using lubridate package but doesn't seem to help mdy_hm(df_test$d1) This resulted in a warning message and resulted in NAs for invalid values It's fine if invalid dates can't be processed and returns