簡體   English   中英

寫入歐洲 CSV,小數點轉換為普通數據框,但不使用“聚合”數據框

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

雖然像下面的“test”這樣的普通數據框可以完美地轉換十進制“.”。 到 ”,”:

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 = ";")

我的另一個數據框沒有以“,”作為十進制出現。 我猜測“grep”和“aggregate”的上游使用不知何故是轉換的障礙。 下面的 Str 輸出,當我有五個時說三個變量。 如何准備數據框以供十進制轉換訪問?

   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"

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")

@r2evans 有正確的預感。 問題是test$x是一個矩陣,這就是問題的根源。 你得到的輸出根本沒有意義,也不能很好地代表數據。 如果您將矩陣列直接包含在數據框中,它會按預期工作。

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")

查看輸出我們發現它不能很好地代表數據:

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 = ";")

reprex 包(v2.0.1) 於 2021 年 10 月 13 日創建

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM