r语言 - 写入欧洲 CSV,小数转换为普通数据框,但不转换为"aggregated"数据框



当一个正常的数据帧,如"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] # extract the first two columns
newdf <- cbind(newdf, test$x) # Add `test$x` as 4 new columns
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)在2018-10-13上创建

相关内容

  • 没有找到相关文章

最新更新