Search code examples
rdatatablecell

How to add a string to each cell of a row in a R data table?


I'm facing an issue which looks very basic, but I'm not able to find the solution.

I have a simple table :

Statut occupation 1983 1988 1996 2002 2007 2012 2017
Propriétaire du logement 207 267 305 363 468 597 482
Locataire 35 40 33 52 50 61 60
Locataire de l'habitat social (OPH, OTHS) 0 0 0 0 0 2 0
Logé gratuitement (parents, amis, employeurs) 39 47 69 99 57 87 98
Total général 281 354 407 514 575 745 640

I want to get this result :

Statut occupation 1983 1988 1996 2002 2007 2012 2017
Propriétaire du logement 207 267 305 363 468 597 482
Locataire 35 40 33 52 50 61 60
Locataire de l'habitat social (OPH, OTHS) 0 0 0 0 0 2 0
Logé gratuitement (parents, amis, employeurs) 39 47 69 99 57 87 98
Total général 281 354 407 514 575 745 640

The purpose is just add a formatting (italic, underline, add unbreakable spaces...) on all the cells of one row. It looks like it's not that easy in R.

What I've tried

I tried to get the name if each column and modify the cell corresponding in a for loop

n.row=3
cols=colnames(Y)
for (i in 1:ncol(Y)){
  Y[n.row,get(cols[i])]<-as.data.table(sprintf("*%s*",as.character(Y[n.row,get(cols[i])])))
}

The problem here, is that Y[n.row,get(cols[i])] always return "Statut occupation" (the column name), whatever the value of n.row. Why ?

I also tried to make it working with the id of the column directly :

n.row=3
for (i in 1:ncol(Y)){
  Y[n.row,..i]<-sprintf("*%s*",Y[n.row,..i])
}

Here :

  • Y[n.row,..i] is giving me the proper information...
  • sprintf("%s",as.character(Y[n.row,..i])) is giving the proper string whatever the class of the column...
  • But Y[n.row,..i]<-sprintf("%s",as.character(Y[n.row,..i])) returns

Error in [<-.data.table(*tmp*, n.row, ..i, value = " Locataire de l'habitat social (OPH, OTHS)") : object '..i' not found

I don't understand the behaviour here. All information is properly findable one by one but I cannot assign one to another because it's suddenly not findable anymre.

Any explanation would be appreciated, or maybe I'm not using to proper strategy to do what I need :) !

Thanks for your help !


Solution

  • Notice that *tmp* is a character, so all columns should keep the same type.

    library(data.table)
    Y <- fread("Statut occupation   1983    1988    1996    2002    2007    2012    2017
    Propriétaire du logement    207 267 305 363 468 597 482
    Locataire   35  40  33  52  50  61  60
    Locataire de l'habitat social (OPH, OTHS)   0   0   0   0   0   2   0
    Logé gratuitement (parents, amis, employeurs)   39  47  69  99  57  87  98
    Total général   281 354 407 514 575 745 640",header = T,colClasses = 'character')
    
    n.row <- 3
    
    Y[n.row, names(Y) := as.list(sprintf("*%s*",Y[n.row,]))]
    

    output:

                                   Statut occupation   1983   1988   1996   2002   2007   2012   2017
                                              <char> <char> <char> <char> <char> <char> <char> <char>
    1:                      Propriétaire du logement    207    267    305    363    468    597    482
    2:                                     Locataire     35     40     33     52     50     61     60
    3:   *Locataire de l'habitat social (OPH, OTHS)*    *0*    *0*    *0*    *0*    *0*    *2*    *0*
    4: Logé gratuitement (parents, amis, employeurs)     39     47     69     99     57     87     98
    5:                                 Total général    281    354    407    514    575    745    640