4

I used jsonlite library: json_pca_data <- toJSON(pca_table, pretty = TRUE)) to produce JSON:

{
    "neuron_type" : [
        "Ciliated_sensory_neurons",
        "Touch_receptor_neurons",
        ...
        ...
    ],
    "PC1" : [
        4.1158,
        -1.1647,
        ...
        ...
    ],
    "PC2" : [
        -1.4615,
        1.9541,
        ...
        ...
    ],
    "octr-1" : [
        2.5517,
        2.8857,
        ...
        ...

from the pca_table that looks like that:

neuron_type                      PC1              PC2          octr-1    
Ciliated_sensory_neurons      4.1157653       -1.4614620      2.551738 
Touch_receptor_neurons       -1.1647174        1.9540974      2.885656 
...
...

But I want to add to the final JSON also PC1: 0.36 and PC2: 0.21. I can just modify the initial pca_table:

pca_table$PC1_percent = percent[1]
pca_table$PC2_percent = percent[2]

Adding two columns and then transforming it into JSON, however, I do not like to have

"PC1_percent" : [
    0.3676,
    0.3676,
    0.3676,
    0.3676,
    0.3676,
    0.3676,
    0.3676,
    0.3676,
    0.3676
],
"PC2_percent" : [
    0.2331,
    0.2331,
    0.2331,
    0.2331,
    0.2331,
    0.2331,
    0.2331,
    0.2331,
    0.2331
]

I want to have just key and value instead:

"PC1_percent" : 0.3676, "PC2_percent" : 0.2331

Is there a way to do that in Rstudio?

r2evans
  • 141,215
  • 6
  • 77
  • 149
Nikita Vlasenko
  • 4,004
  • 7
  • 47
  • 87
  • 1
    (1) you are *replacing* with `<-`, not appending, perhaps you intend to use `pca_table$PC1_percent = c(pca_table$PC1_percent, percent[1])`; (2) this is problematic with data.frames, since you will be extending all columns, so if you intend to add a value to all columns, consider creating a new data.frame and using `rbind`; if you intend to just extend those two values, then convert from data.frame with `as.list` and append there. – r2evans Dec 26 '17 at 19:49
  • 1
    Regardless, it is my suggestion to add to the underlying data and then convert back into JSON vice trying to add to the textual JSON representation. – r2evans Dec 26 '17 at 19:49
  • The problem is that I do not understand how to add it to initial dataset, so that there would be no expansion to an array. And I need to just create new columns and that is what I am doing, neither appending to already present column, nor replacing. – Nikita Vlasenko Dec 26 '17 at 20:00

1 Answers1

4

I agree with your logic to change the underlying data vice editing JSON. Your first problem is that you are replacing the column values with a single one, instead of appending it.

pca_table$PC1_percent = c(pca_table$PC1_percent, percent[1])

would append the first value of percent onto the end of the PC1_percent column. Unfortunately, this will likely error (something like replacement has 3 rows, data has 2) or will produce other undesirable effects.

It helps to know if you intend to append a value to all columns or if you just want to extend those two rows. For both methods, my fake data:

tbl <- data.frame(pc1=1:2, pc2=11:12, othr=51)
tbl
#   pc1 pc2 othr
# 1   1  11   51
# 2   2  12   51

Extend all columns

Make a new row, representative of the original data.frame (you need to include something for all columns):

newrow <- data.frame(pc1=3, pc2=13, othr=NA)
tbl2 <- rbind(tbl, newrow)
tbl2
#   pc1 pc2 othr
# 1   1  11   51
# 2   2  12   51
# 3   3  13   NA
jsonlite::toJSON(tbl2, dataframe="columns")
# {"pc1":[1,2,3],"pc2":[11,12,13],"othr":[51,51,"NA"]} 

(I'm inferring "columns" from your problem, even if you didn't specify that in your call. I'm omitting pretty=TRUE for brevity.)

Extend only some columns

By going this route, you change from a data.frame to a list, since a data.frame is roughly a list of vectors where all vectors are the same length.

lst <- as.list(tbl)
lst$pc1 <- c(lst$pc1, 98)
lst$pc2 <- c(lst$pc2, 99)
jsonlite::toJSON(lst)
# {"pc1":[1,2,98],"pc2":[11,12,99],"othr":[51,51]} 
r2evans
  • 141,215
  • 6
  • 77
  • 149