简体   繁体   English

写入欧洲 CSV,小数点转换为普通数据框,但不使用“聚合”数据框

[英]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 "."虽然像下面的“test”这样的普通数据框可以完美地转换十进制“.”。 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.我猜测“grep”和“aggregate”的上游使用不知何故是转换的障碍。 Str output below, says three variables when I have five.下面的 Str 输出,当我有五个时说三个变量。 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 . 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 had the right hunch. @r2evans 有正确的预感。 The problem is test$x which is a matrix and that is the source of the problem.问题是test$x是一个矩阵,这就是问题的根源。 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)reprex 包(v2.0.1) 于 2021 年 10 月 13 日创建

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM