Creating row in dataframe for each element in vector

I have a vector of numbers:

 a <- c(54, 456, 23432, 4868, 34, 245634, 37, 46453, 1342354)

In my already-existent dataframe (head included via dput below), I would like to create a new variable. Each row of the new variable will contain a single element from the vector. So there would be one value (e.g. 54) in each row of the new variable.

    structure(list(Phone = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L), .Label = "a", class = "factor"), Frame = structure(c(1L, 
    3L, 2L, 4L, 6L, 5L), .Label = c("[-4.46225397 -4.14727267 -4.45203785 -4.67251549 -5.13750066 -4.92839463\n -5.03957588 -5.68530479]", 
    "[-6.14532579 -4.38918589 -4.12275354 -4.19263549 -4.30380823 -4.35621995\n -4.4079389  -4.47339504]", 
    "[-6.43104195 -4.75506178 -4.2324676  -4.21878988 -4.1635973  -4.11186806\n -4.05023489 -4.08204198]", 
    "[-7.1528423  -5.46190925 -5.94873845 -6.635839   -6.84179002 -6.85955335\n -6.83714326 -6.87621415]", 
    "[-7.23901353 -4.61522546 -3.25206619 -3.38407075 -3.63762837 -3.85352927\n -3.94250123 -4.04015791]", 
    "[-7.34451319 -5.58664694 -4.69929752 -4.621823   -4.51670576 -4.48494125\n -4.39512713 -4.26553646]"
    ), class = "factor"), Previous = structure(c(1L, 1L, 1L, 1L, 
    1L, 1L), .Label = "ch", class = "factor"), Following = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L), .Label = "p", class = "factor"), Word = structure(c(1L, 
    1L, 1L, 1L, 1L, 1L), .Label = "juk'ucha-pi", class = "factor"), 
Note = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "", class = "factor"), 
"[-10.79197258  -7.97949955  -7.10253093  -7.07957825  -6.98695923\n  -6.90015207  -6.79672506  -6.85010073", 
"[-10.31251047  -7.36552088  -6.91841906  -7.0356884   -7.2222481\n  -7.31020053  -7.39699043  -7.5068328 ", 
"[-12.00323036  -9.16566481  -9.982616   -11.13564383 -11.48125155\n -11.51106031 -11.47345379 -11.5390189 ", 
"[-12.32487451  -9.37498793  -7.8859212   -7.7559107   -7.5795128\n  -7.52620857  -7.37549093  -7.15802398", 
"[-12.14783486  -7.74483933  -5.45731306  -5.67883075  -6.10432742\n  -6.46663209  -6.61593651  -6.77981481"
), Morph_status = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = "", class = "factor"), 
 row.names = c(NA, 6L), class = "data.frame")

1 answer

  • answered 2018-12-05 20:09 Julius Vainora

    When working with data frames, each variable (column) has as many entries as there are rows. What you are describing then is not a data frame and, if I understand you question correctly, the best your can do is going back to general lists:

    df <- data.frame(a = 1:3, b = 1:3)
    c(as.list(df), c = list(a))
    # $a
    # [1] 1 2 3
    #
    # $b
    # [1] 1 2 3
    #
    # $c
    # [1]      54     456   23432    4868      34  245634      37   46453 1342354
    

    One other option, as to still have a data frame, would be to fill all the shorter columns with NA's:

    library(rowr)
    cbind.fill(df, a, fill = NA)
    #    a  b  object
    # 1  1  1      54
    # 2  2  2     456
    # 3  3  3   23432
    # 4 NA NA    4868
    # 5 NA NA      34
    # 6 NA NA  245634
    # 7 NA NA      37
    # 8 NA NA   46453
    # 9 NA NA 1342354