data.table решение
library(data.table)
пример данных
df <- structure(list(Name = structure(c(2L, 1L, 4L, 3L), .Label = c("Ademulegun, Sauel Adesujo", "Bassey, Wellington Umo", "Imo, U. O.", "Lawan, Umar"), class = "factor"), Mons = c(0L, 0L, 0L, 0L), Sandhurst = c(0L, 0L, 1L, 0L), Entry = structure(c(2L, 3L, 1L, 4L), .Label = c("2/6/1953", "4/30/1949", "6/11/1949", "6/4/1955"), class = "factor"), Second.Lieutenant = structure(c(2L, 3L, 1L, 4L), .Label = c("2/6/1953", "4/30/1949", "6/11/1949", "6/4/1955"), class = "factor"), Lieutenant = structure(c(2L, 1L, 4L, 3L), .Label = c("12/20/1949", "4/30/1949", "5/3/1958", "8/1/1955"), class = "factor"), Captain = structure(c(2L, 3L, 1L, 4L), .Label = c("", "2/7/1951", "3/5/1952", "5/3/1958"), class = "factor"), Major = structure(c(4L, 3L, 1L, 2L), .Label = c("", "1/15/1963", "12/27/1958", "6/21/1957" ), class = "factor"), Lieutenant.Colonel = structure(c(4L, 3L, 1L, 2L), .Label = c("", "1/15/1963", "10/3/1962", "8/30/1962" ), class = "factor"), Colonel = structure(c(3L, 2L, 1L, 1L ), .Label = c("", "10/3/1962", "2/26/1966"), class = "factor"), Brigadier.General = structure(c(3L, 2L, 1L, 1L), .Label = c("", "10/3/1962", "2/26/1966"), class = "factor"), Depature = structure(c(2L, 1L, 3L, 4L), .Label = c("1/15/1966", "11/1/1966", "5/8/1956", "7/6/1967"), class = "factor"), ethnicity = structure(c(1L, 4L, 3L, 2L), .Label = c("Efik", "Igbo", "Kanuri", "Yoruba" ), class = "factor")), class = "data.frame", row.names = c(NA, -4L))
изменить с использованием data.tables fast melt
library( data.table )
data.table::melt( data = setDT( df ),
id.vars = c("Name", "Mons", "Sandhurst", "ethnicity" ),
value.name = "Date",
variable.name = "Rank",
na.rm = TRUE )
результат
# Name Mons Sandhurst ethnicity Rank Date
# 1: Bassey, Wellington Umo 0 0 Efik Entry 4/30/1949
# 2: Ademulegun, Sauel Adesujo 0 0 Yoruba Entry 6/11/1949
# 3: Lawan, Umar 0 1 Kanuri Entry 2/6/1953
# 4: Imo, U. O. 0 0 Igbo Entry 6/4/1955
# 5: Bassey, Wellington Umo 0 0 Efik Second.Lieutenant 4/30/1949
# 6: Ademulegun, Sauel Adesujo 0 0 Yoruba Second.Lieutenant 6/11/1949
# 7: Lawan, Umar 0 1 Kanuri Second.Lieutenant 2/6/1953
# 8: Imo, U. O. 0 0 Igbo Second.Lieutenant 6/4/1955
# 9: Bassey, Wellington Umo 0 0 Efik Lieutenant 4/30/1949
изменить порядок по мере необходимости ...
примечание: Если вы хотите «пустой»Даты исчезают в расплавленных данных, убедитесь, что в исходных данных заданы значения NA
в пустых датах. na.rm = TRUE
в функции плавления, затем удалите их.