How can I replace the values in a matrix of class table?

46 Views Asked by At

In an R object of class table, how can I replace its values?

In order to replace its names, I can use:

attr( object, "dimnames" )[[1]] <- c("AA", "BB", "CC") 

However, I could not find a way to replace its values! How can I create 1 dimensional table (1d)?

dput structure:

df <-  structure(c(`AL` = 0, `AM` = 2, `AH` = 3), class = "table", .Dim = 3L, .Dimnames = list( c("AL", "AM", "AH")) )

More information

The attributes of the original object looks like that

attributes( df_object )
$class
[1] "table"

$dim
[1] 3

$dimnames
$dimnames[[1]]
[1] "AL" "AM"  "AH" 

Current Output

AL  AM  AH 
0   2    3 

The expected output will be: Note, it must keep all the attributes that had previously:

AL  AM  AH 
10    0    0 
1

There are 1 best solutions below

1
On BEST ANSWER

Use subsetting. For example:

df[1 : 3] = 4 : 6
df
# AL AM AH
#  4  5  6