在R中附加数据的有效方法

时间:2013-06-03 22:59:42

标签: r append

我有几个数据框,我想附加这些数据。可重复的例子如下:

gous <- structure(list(V1 = c(0, 28.44), V2 = c(1, 28.44), V3 = c(2, 
28.44), V4 = c(3, 28.39), V5 = c(4, 28.22), V6 = c(5, 27.72), 
    V7 = c(6, 24.56), V8 = c(7, 18.78), V9 = c(8, 18.5), V10 = c(9, 
    18.56), V11 = c(10, 18.5), V12 = c(11, 18.72)), .Names = c("V1", 
"V2", "V3", "V4", "V5", "V6", "V7", "V8", "V9", "V10", "V11", 
"V12"), class = "data.frame", row.names = c(NA, -2L))

gous1<-  structure(list(V1 = structure(1L, .Label = "GOUS,", class = "factor"), 
    V2 = 479868.2, V3 = 3723003.3), .Names = c("V1", "V2", "V3"
), class = "data.frame", row.names = c(NA, -1L))

coordgous <- paste0(gous1$V2,",",gous1$V3)
gomz <-     structure(list(V1 = c(0, 29.28), V2 = c(1, 29.22), V3 = c(2, 
29.11), V4 = c(3, 28.94), V5 = c(4, 28.44), V6 = c(5, 27.61), 
    V7 = c(6, 26.89), V8 = c(7, 25.22), V9 = c(8, 23.56), V10 = c(9, 
    22.72), V11 = c(10, 22.44), V12 = c(11, 22.28)), .Names = c("V1", 
"V2", "V3", "V4", "V5", "V6", "V7", "V8", "V9", "V10", "V11", 
"V12"), class = "data.frame", row.names = c(NA, -2L))

gomz1 <- structure(list(V1 = structure(1L, .Label = "Mixing", class = "factor"), 
    V2 = structure(1L, .Label = "Zone,", class = "factor"), V3 = 479868.2, 
    V4 = 3723003.3), .Names = c("V1", "V2", "V3", "V4"), class = "data.frame", row.names = c(NA, 
-1L))

coordgomz <- paste0(gomz1$V3,",",gomz1$V4)

ari <- structure(list(V1 = c(0, 29.22), V2 = c(1, 29.17), V3 = c(2, 
29.11), V4 = c(3, 28.83), V5 = c(4, 28.28), V6 = c(5, 27.5), 
    V7 = c(6, 26.17), V8 = c(7, 19.89), V9 = c(8, 20.11)), .Names = c("V1", 
"V2", "V3", "V4", "V5", "V6", "V7", "V8", "V9"), class = "data.frame", row.names = c(NA, 
-2L))

ari1 <- structure(list(V1 = structure(1L, .Label = "AboveRiverIntake,", class = "factor"), 
    V2 = 482631.6, V3 = 3722791.3), .Names = c("V1", "V2", "V3"
), class = "data.frame", row.names = c(NA, -1L))

coordari <- paste0(ari1$V2,",",ari1$V3)

gods <- structure(list(V1 = c(0, 28.61), V2 = c(1, 28.56), V3 = c(2, 
28.56), V4 = c(3, 28.5), V5 = c(4, 28.06), V6 = c(5, 27.83), 
    V7 = c(6, 27.56), V8 = c(7, 27.44), V9 = c(8, 26.17), V10 = c(9, 
    25.17)), .Names = c("V1", "V2", "V3", "V4", "V5", "V6", "V7", 
"V8", "V9", "V10"), class = "data.frame", row.names = c(NA, -2L
))

gods1 <- structure(list(V1 = structure(1L, .Label = "Downstream,", class = "factor"), 
    V2 = 480553.1, V3 = 3720785), .Names = c("V1", "V2", "V3"
), class = "data.frame", row.names = c(NA, -1L))

coordgods <- paste0(gods1$V2,",",gods1$V3)

我用来附加数据的代码如下:

new <- c("data", "up")
new0 <- append(new,coordgous)
new1 <- append(new0,c(t(gous)),after=length(new0))
new2 <- append(new1,"MZ",after=length(new1))
new3 <- append(new2,coordgomz,after=length(new2))
new4<-  append(new3,c(t(gomz)),after=length(new3))
new5 <- append(new4,"ARI",after=length(new4))
new6 <- append(new5,coordari,after=length(new5))
new7 <- append(new6,c(t(ari)),after=length(new6))
new8 <- append(new7,"DS",after=length(new7))
new9 <- append(new8,coordgods,after=length(new8))
new10 <- append(new9, c(t(gods)), after=length(new9))

我使用的方法效果很好,但似乎不是有效的方法。必须有一种有效的方法来做到这一点。

所需的最终输出如下:

> print(new10,quote=F)
 [1] data               up                 479868.2,3723003.3 0                  1                  2                  3                 
 [8] 4                  5                  6                  7                  8                  9                  10                
[15] 11                 28.44              28.44              28.44              28.39              28.22              27.72             
[22] 24.56              18.78              18.5               18.56              18.5               18.72              MZ                
[29] 479868.2,3723003.3 0                  1                  2                  3                  4                  5                 
[36] 6                  7                  8                  9                  10                 11                 29.28             
[43] 29.22              29.11              28.94              28.44              27.61              26.89              25.22             
[50] 23.56              22.72              22.44              22.28              ARI                482631.6,3722791.3 0                 
[57] 1                  2                  3                  4                  5                  6                  7                 
[64] 8                  29.22              29.17              29.11              28.83              28.28              27.5              
[71] 26.17              19.89              20.11              DS                 480553.1,3720785   0                  1                 
[78] 2                  3                  4                  5                  6                  7                  8                 
[85] 9                  28.61              28.56              28.56              28.5               28.06              27.83             
[92] 27.56              27.44              26.17              25.17 

2 个答案:

答案 0 :(得分:3)

我感觉你正在通过这种策略丢失信息。你有4组数据。按名称对数据进行分组可能更有意义:

 newdat <- list(US = list( coordgous, t(gous) ),
                MZ = list( coordgomz, t(gomz) ),
                ARI = list( coordari, t(ari) )
                DS = list(coordgods, t(gods) ) )

然后你可以“lapply”每个项目“

 > cords <- sapply(newdat, "[", 1)
> cords
$US
[1] "479868.2,3723003.3"

$MZ
[1] "479868.2,3723003.3"

$ARI
[1] "482631.6,3722791.3"

$DS
[1] "480553.1,3720785"

...或按名称引用:

 newdat[["US"]]
#---------
$US
$US[[1]]
[1] "479868.2,3723003.3"

$US[[2]]
    [,1]  [,2]
V1     0 28.44
V2     1 28.44
V3     2 28.44
V4     3 28.39
V5     4 28.22
V6     5 27.72
V7     6 24.56
V8     7 18.78
V9     8 18.50
V10    9 18.56
V11   10 18.50
V12   11 18.72

答案 1 :(得分:1)

只需使用c一起完成所有操作:

new10 <- c("data", "up", coordgous,c(t(gous)),"MZ",coordgomz,c(t(gomz)),"ARI",coordari,c(t(ari)),"DS",coordgods,c(t(gods)))

(原始代码中留下了一些免费的c来电。)