Write to european CSV, decimals converted for normal data frame but not with "aggregated" data frame

While a normal data frame such as "test" below, works perfectly in converting decimal "." to ",":

a <- c(1:34)
b <- rnorm(34, mean=33, sd=7)
test <- cbind.data.frame(a,b)
write.table(file="test.csv",test, row.names = F, dec=",", sep = ";")

My other data frame does not come out with "," as decimal. I am guessing that the upstream usage of "grep" and "aggregate" somehow is an obstacle for the conversion. Str output below, says three variables when I have five. How do I prepare the data frame to be accessable for decimal conversion?

   Group.1                Group.2       x.mean         x.sd         x.cv
1       P1      Compound 1:  IgG1  11.94520000   0.11435889   0.95736270
2       P2      Compound 1:  IgG1  10.29220000   0.06536700   0.63511201
3       P1      Compound 2:  IgG2  10.07450000   0.05682967   0.56409417
4       P2      Compound 2:  IgG2  19.66320000   0.16354259   0.83171908
...



'data.frame':   12 obs. of  3 variables:
$ Group.1: Factor w/ 10 levels "","FBS","ID",..: 9 10 9 10 9 10 9 10 9 10 ...
$ Group.2: Factor w/ 11 levels "Compound 1:  IgG1",..: 1 1 2 2 3 3 4 4 5 5 ...
$ x      : num [1:12, 1:3] 11.95 10.29 10.07 19.66 4.21 ...
 ..- attr(*, "dimnames")=List of 2
 .. ..$ : NULL
 .. ..$ : chr  "mean" "sd" "cv"

Output from dput.

structure(list(Group.1 = structure(c(9L, 10L, 9L, 10L, 9L, 10L
), .Label = c("", "FBS", "ID", "K1", "K2", "K3", "K4", "K5", 
"P1", "P2"), class = "factor"), Group.2 = structure(c(1L, 1L, 
2L, 2L, 3L, 3L), .Label = c("Compound 1:  IgG1", "Compound 2:  IgG2", 
"Compound 3:  IgG3", "Compound 4:  IgG3-723", "Compound 5:  IgG4", 
"Compound 6:  Total-IgG", "Compound 7:  IgG1_IS", "Compound 8:  IgG2_IS", 
"Compound 9:  IgG3_IS", "Compound 10:  IgG4_IS", "Compound 11:  Total_IgG_IS"
), class = "factor"), x = structure(c(11.9452, 10.2922, 10.0745, 
19.6632, 4.2135, 3.7465, 0.114358889272131, 0.0653669981293651, 
0.0568296675259594, 0.163542587046242, 0.0569370997973496, 0.0253651116474753, 
0.957362700265639, 0.63511200840797, 0.564094173665784, 0.831719084616146, 
1.35130176331671, 0.677034876484061), .Dim = c(6L, 3L), .Dimnames = list(
    NULL, c("mean", "sd", "cv")))), row.names = c(NA, 6L), class = "data.frame")

1 answer

  • answered 2021-10-13 07:29 Jan

    @r2evans had the right hunch. The problem is test$x which is a matrix and that is the source of the problem. The output you get does not make sense at all and does not represent the data well. If you include the matrix columns directly into the data frame, it works as expected.

    test <- structure(
      list(Group.1 = structure(
        c(9L, 10L, 9L, 10L, 9L, 10L), 
        .Label = c("", "FBS", "ID", "K1", "K2", "K3", "K4", "K5", "P1", "P2"), 
        class = "factor"), 
        Group.2 = structure(
          c(1L, 1L, 2L, 2L, 3L, 3L), 
          .Label = c("Compound 1:  IgG1", "Compound 2:  IgG2", "Compound 3:  IgG3", 
                     "Compound 4:  IgG3-723", "Compound 5:  IgG4", "Compound 6:  Total-IgG", 
                     "Compound 7:  IgG1_IS", "Compound 8:  IgG2_IS", "Compound 9:  IgG3_IS", 
                     "Compound 10:  IgG4_IS", "Compound 11:  Total_IgG_IS"), 
          class = "factor"), 
        x = structure(c(11.9452, 10.2922, 10.0745, 19.6632, 4.2135, 3.7465, 0.114358889272131, 0.0653669981293651, 
                        0.0568296675259594, 0.163542587046242, 0.0569370997973496, 0.0253651116474753, 
                        0.957362700265639, 0.63511200840797, 0.564094173665784, 0.831719084616146, 
                        1.35130176331671, 0.677034876484061), 
                      .Dim = c(6L, 3L), 
                      .Dimnames = list(NULL, c("mean", "sd", "cv")))), 
      row.names = c(NA, 6L), 
      class = "data.frame")
    

    Looking at the output we see that it does not represent the data well:

    test
    #>   Group.1           Group.2      x.mean        x.sd        x.cv
    #> 1      P1 Compound 1:  IgG1 11.94520000  0.11435889  0.95736270
    #> 2      P2 Compound 1:  IgG1 10.29220000  0.06536700  0.63511201
    #> 3      P1 Compound 2:  IgG2 10.07450000  0.05682967  0.56409417
    #> 4      P2 Compound 2:  IgG2 19.66320000  0.16354259  0.83171908
    #> 5      P1 Compound 3:  IgG3  4.21350000  0.05693710  1.35130176
    #> 6      P2 Compound 3:  IgG3  3.74650000  0.02536511  0.67703488
    write.table(file="test.csv", test, row.names = F, dec=",", sep = ";")
    
    newdf <- test[1:2]
    newdf <- cbind(newdf, test$x)
    newdf
    #>   Group.1           Group.2    mean         sd        cv
    #> 1      P1 Compound 1:  IgG1 11.9452 0.11435889 0.9573627
    #> 2      P2 Compound 1:  IgG1 10.2922 0.06536700 0.6351120
    #> 3      P1 Compound 2:  IgG2 10.0745 0.05682967 0.5640942
    #> 4      P2 Compound 2:  IgG2 19.6632 0.16354259 0.8317191
    #> 5      P1 Compound 3:  IgG3  4.2135 0.05693710 1.3513018
    #> 6      P2 Compound 3:  IgG3  3.7465 0.02536511 0.6770349
    write.table(file="test.csv", newdf, row.names = F, dec=",", sep = ";")
    

    Created on 2021-10-13 by the reprex package (v2.0.1)

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum