Search code examples
rdplyrdata.tabletidyverse

Iteration over data.table


I have a table A with column names: Var1, Var2, Var3.

Var1 = c("N1", "N2", "0", "0", "N3", "N4", "0", "0")
Var2 = c("0", "A", "0", "0", "0", "B", "0", "0")
Var3 = c("0", "Yes", "No", "All", "0", "x", "y", "z")

I would like to obtain vectors based on Table A, which contains values from column eg: N2 = (Yes, No, All), N4 = (x, y, z). I have tried few iterations with "for loop" and "logical if" but with no success. Please, give me some hint.


Solution

  • With data.table:

    • replace the 0s in Var1 by NA
    • carry forward last occurence of non NA values (using for example zoo::na.locf because data.table::nafill doesnt yet work for characters).
    • filter according to Var1:
    library(data.table)
    data <- data.table(Var1 = c("N1", "N2", "0", "0", "N3", "N4", "0", "0"), Var2 = c("0", "A", "0", "0", "0", "B", "0", "0"), Var3 = c("0", "Yes", "No", "All", "0", "x", "y", "z"))
    
    # replace 0s by NA for next step
    data[Var1==0,Var1:=NA]
    
    # last occurence carried forward
    data[,Var1:=zoo::na.locf(Var1)]
    
    data[Var1=='N2',Var3]
    #[1] "Yes" "No"  "All"
    
    data[Var1=='N4',Var3]
    #[1] "x" "y" "z"