Home > Mobile >  How to add a row to a dataframe and populate it with a string?
How to add a row to a dataframe and populate it with a string?

Time:01-22

I want add a row to my df and populate the row with the normal string.

df[nrow(df)   1,] <- "normal"

Traceback:

Error in `[<-`(`*tmp*`, nrow(df)   1, , value = "normal") : 
  subscript out of bounds

Data:

> dput(df)
structure(c(0.817235177513274, 0.842271224674921, 0.787053698556768, 
0.723253823258161, 0.330810989820991, 0.929282245774236, 0.385900089441395, 
0.580292714010189, 0.928938997366233, 0.78643829926366, 0.739342773828268, 
0.815944692823519, 0.701024754855706, 0.690557534921593, 0.292246602221495, 
0.907803873079535, 0.453213181805882, 0.550745234719392, 0.913370916956376, 
0.716310393262212, 0.765542822762287, 0.568705595673652, 0.6714274267581, 
0.531716650540856, 0.331580044004539, 0.840889607944185, 0.494640734908958, 
0.396783055306106, 0.8692727119489, 0.759022283299301, 0.787291380662438, 
0.855192302325311, 0.743632726648944, 0.710242442123609, 0.351783150991687, 
0.933702108520557, 0.410525438050587, 0.549479034093077, 0.920910305577791, 
0.812458352284617, 0.762864102512286, 0.82250047660344, 0.681146614449272, 
0.722562365641304, 0.295007368926608, 0.923671059571924, 0.379539671884597, 
0.515060486276735, 0.91767133328213, 0.79917575064228, 0.743564108418945, 
0.541783705139426, 0.675695188150901, 0.530335228219673, 0.311148090618456, 
0.888403586041492, 0.489850949026585, 0.495137719924742, 0.928270455994818, 
0.781619937522964, 0.83114689516785, 0.745890503970576, 0.819047883913701, 
0.693061612485807, 0.34437655946559, 0.925122536085658, 0.438053894428271, 
0.539601859327891, 0.930313177385918, 0.77987407757735, 0.797258263299495, 
0.539125161286865, 0.73772463742416, 0.596959758946811, 0.340833820254246, 
0.867012396000705, 0.414398893790568, 0.478458869251912, 0.9148617831338, 
0.794732448316904, 0.750972160691783, 0.421962727382347, 0.630243285799926, 
0.575866112480541, 0.331617484056072, 0.880175147517098, 0.398626259709664, 
0.453109749361369, 0.89591402846511, 0.800528147561602, 0.797443406632106, 
0.707871104991111, 0.720883316856463, 0.589889378319629, 0.349813234586068, 
0.740142906790019, 0.495298129407806, 0.389674692905969, 0.869933439187107, 
0.743658144096678), dim = c(10L, 10L), dimnames = list(c("ALDH8A1", 
"ALDOB", "ANXA9", "AQP1", "ARL4D", "BIN2", "BRF2", "BST2", "C14orf109", 
"C1QTNF3"), c("TCGA-CZ-5455-11A", "TCGA-BQ-7061-11A", "TCGA-B0-5402-11A", 
"TCGA-CZ-5469-11A", "TCGA-CZ-5465-11A", "TCGA-BQ-7051-11A", "TCGA-CZ-5454-11A", 
"TCGA-BQ-5875-11A", "TCGA-BQ-5891-11A", "TCGA-BQ-5890-11A")))

CodePudding user response:

  1. As RicVallalba commented, your data is a matrix and not a data.frame. This is generally not a problem, we can work either way, but if you need this to be a frame, then do df <- as.data.frame(df).

  2. It isn't clear what you mean by appending "normal" (a string) to an object filled with numeric data: by adding a string to any of the columns, you corrupt said columns by converting those numbers into strings, defeating any number-operations you may want to do on them in the future.

    I'm going to infer that what you really mean is to add a row named "normal" with empty values.

    dfnew <- rbind(df, `rownames<-`(df[1,,drop=FALSE][NA,,drop=FALSE], "normal"))
    dfnew
    #           TCGA-CZ-5455-11A TCGA-BQ-7061-11A TCGA-B0-5402-11A TCGA-CZ-5469-11A TCGA-CZ-5465-11A TCGA-BQ-7051-11A TCGA-CZ-5454-11A TCGA-BQ-5875-11A TCGA-BQ-5891-11A TCGA-BQ-5890-11A
    # ALDH8A1          0.8172352        0.7393428        0.7655428        0.7872914        0.7628641        0.7435641        0.8311469        0.7972583        0.7509722        0.7974434
    # ALDOB            0.8422712        0.8159447        0.5687056        0.8551923        0.8225005        0.5417837        0.7458905        0.5391252        0.4219627        0.7078711
    # ANXA9            0.7870537        0.7010248        0.6714274        0.7436327        0.6811466        0.6756952        0.8190479        0.7377246        0.6302433        0.7208833
    # AQP1             0.7232538        0.6905575        0.5317167        0.7102424        0.7225624        0.5303352        0.6930616        0.5969598        0.5758661        0.5898894
    # ARL4D            0.3308110        0.2922466        0.3315800        0.3517832        0.2950074        0.3111481        0.3443766        0.3408338        0.3316175        0.3498132
    # BIN2             0.9292822        0.9078039        0.8408896        0.9337021        0.9236711        0.8884036        0.9251225        0.8670124        0.8801751        0.7401429
    # BRF2             0.3859001        0.4532132        0.4946407        0.4105254        0.3795397        0.4898509        0.4380539        0.4143989        0.3986263        0.4952981
    # BST2             0.5802927        0.5507452        0.3967831        0.5494790        0.5150605        0.4951377        0.5396019        0.4784589        0.4531097        0.3896747
    # C14orf109        0.9289390        0.9133709        0.8692727        0.9209103        0.9176713        0.9282705        0.9303132        0.9148618        0.8959140        0.8699334
    # C1QTNF3          0.7864383        0.7163104        0.7590223        0.8124584        0.7991758        0.7816199        0.7798741        0.7947324        0.8005281        0.7436581
    # normal                  NA               NA               NA               NA               NA               NA               NA               NA               NA               NA
    

    (This works with both a frame and a matrix.)

  •  Tags:  
  • r
  • Related