-1

更新:来自 dput(ldf[[1]]) 的结果

没问题。这是:“A 04/18/2013 06:34:58 3D9.1C2D9F22C2”、“A 04/18/2013 06:34:58 3D9.1C2D9F22C2”、“A 04/18/2013 06:38:24 3D9.1C2DDAE977”、“A 04/18/2013 06:42:38 3D9.1C2DA0E0B5”、“A 04/18/2013 06:42:38 3D9.1C2DA0E0B5”、“A 04/18/2013 07:07: 49 3D9.1C2DD9D3CF","A 04/18/2013 07:07:49 3D9.1C2DD9D3CF")

  • 问题可能在于这 4 个变量的某些行不完整。*---------------------------------------------------- ----------------------------

我从这个论坛得到了很多这个难题,但我仍然被卡住了。我正在尝试遍历 30 个数据帧的列表,这些数据帧的数据是从文本文件中读取的。在循环结束时,我不断收到一条错误消息和一个空的目标数据帧。谁能看出问题出在哪里?

以下是一些示例数据:

[73] "E 04/21/2013 14:05:01 3D9.1C2DF6F22D" "E 04/21/2013 14:05:01 3D9.1C2DF6F22D"
[75] "E 04/21/2013 14:47:54 3D9.1C2DF6F22D" "E 04/21/2013 14:47:54 3D9.1C2DF6F22D"

[[26]]
[1] "E 04/22/2013 17:07:02 3D9.1C2DDAC745" "E 04/22/2013 17:07:02 3D9.1C2DDAC745"
[3] "E 04/22/2013 17:07:02 3D9.1C2DDAC745"

[[27]]
[1] "F 04/17/2013 15:14:39 3D9.1C2D1DB26E" "F 04/17/2013 15:14:43 3D9.1C2D1DB26E"
[3] "F 04/17/2013 15:14:43 3D9.1C2D1DB26E" "F 04/17/2013 15:14:43 3D9.1C2D1DB26E"

这是我的循环代码:

new <- data.frame()

for (i in 1:length(ldf)) {
 a[i] <- as.data.frame(ldf[i])
 a[i] <- as.data.frame(a[i][-1,])
 names(a[i]) <- "id"
 c[i] <- strsplit(as.character(a[i]$id)," ")
 reader[i] = sapply(c[i],function(x)x[1])
 date[i] = sapply(c[i],function(x)x[2])
 time[i] = sapply(c[i],function(x)x[3])
 code[i] = sapply(c[i],function(x)x[4])
 out[i] <- as.data.frame(cbind(reader[i],date[i],time[i],code[i]))

new <- rbind(new, out[i])
}

这是我收到的错误消息:

Error in [<-.data.frame(`*tmp*`, i, 
value = list(c..A.04.17.2013.12.24.07.3D9.1C2D1DB26E....A.04.17.2013.12.24.07.3D9.1C2D1DB26E... = c(1L,  
: replacement element 1 has 337 rows, need 394

谢谢!

4

2 回答 2

1

如果我理解正确你想要这个:

ldf <- list(c("E 04/21/2013 14:05:01 3D9.1C2DF6F22D","E 04/21/2013 14:05:01 3D9.1C2DF6F22D","E 04/21/2013 14:47:54 3D9.1C2DF6F22D","E 04/21/2013 14:47:54 3D9.1C2DF6F22D"),
c("E 04/22/2013 17:07:02 3D9.1C2DDAC745","E 04/22/2013 17:07:02 3D9.1C2DDAC745","E 04/22/2013 17:07:02 3D9.1C2DDAC745"),
c("F 04/17/2013 15:14:39 3D9.1C2D1DB26E","F 04/17/2013 15:14:43 3D9.1C2D1DB26E","F 04/17/2013 15:14:43 3D9.1C2D1DB26E","F 04/17/2013 15:14:43 3D9.1C2D1DB26E"))

do.call(rbind,lapply(ldf,function(x) data.frame(do.call(rbind,strsplit(x," ")))))
   X1         X2       X3             X4
1   E 04/21/2013 14:05:01 3D9.1C2DF6F22D
2   E 04/21/2013 14:05:01 3D9.1C2DF6F22D
3   E 04/21/2013 14:47:54 3D9.1C2DF6F22D
4   E 04/21/2013 14:47:54 3D9.1C2DF6F22D
5   E 04/22/2013 17:07:02 3D9.1C2DDAC745
6   E 04/22/2013 17:07:02 3D9.1C2DDAC745
7   E 04/22/2013 17:07:02 3D9.1C2DDAC745
8   F 04/17/2013 15:14:39 3D9.1C2D1DB26E
9   F 04/17/2013 15:14:43 3D9.1C2D1DB26E
10  F 04/17/2013 15:14:43 3D9.1C2D1DB26E
11  F 04/17/2013 15:14:43 3D9.1C2D1DB26E

请注意,所有列都属于类因素。

于 2013-04-25T15:00:33.403 回答
0

ldf的数据框列表是什么?如果是这样,您没有正确索引它们。list您仍在尝试对类对象进行操作。看看这个玩具例子:

L <- list( x=matrix(1:4,nrow=2) , y=matrix(1:4,nrow=2) )
L
#$x
#    [,1] [,2]
#[1,]    1    3
#[2,]    2    4

#$y
#    [,1] [,2]
#[1,]    1    3
#[2,]    2    4

class(L[1])
[1] "list"
class(L[[1]])
[1] "matrix"

您在循环中使用了很多没有多大意义的循环结构,因为它们是作为便利函数提供的,因此您不必使用循环。使用您的数据子集,您可以获得我认为您想要的结果,使用lapply访问列表中的每个 data.frame,然后使用每个数据框apply的列在它们上运行strplit,如下所示:

# Make a list of two data.frames using some of your data
dat1 <- data.frame( x = c("E 04/21/2013 14:05:01 3D9.1C2DF6F22D" , "E 04/21/2013 14:05:01 3D9.1C2DF6F22D") , y = c( "E 04/22/2013 17:07:02 3D9.1C2DDAC745" , "E 04/22/2013 17:07:02 3D9.1C2DDAC745") )
dat2 <- data.frame( x = c("F 04/17/2013 15:14:39 3D9.1C2D1DB26E" , "F 04/17/2013 15:14:43 3D9.1C2D1DB26E") , y = c( "F 04/17/2013 15:14:43 3D9.1C2D1DB26E" , "F 04/17/2013 15:14:43 3D9.1C2D1DB26E") )
dat <- list( dat1 , dat2 )


res <- data.frame( t( data.frame( lapply( dat , function(x) apply( x , 2 , strsplit , " " ) ) ) ) )
names(res) <- c( "Reader" , "Date" , "Time" , "Code" )
rownames(res) <- seq_len( nrow( res ) )
#  Reader       Date     Time           Code
#1      E 04/21/2013 14:05:01 3D9.1C2DF6F22D
#2      E 04/21/2013 14:05:01 3D9.1C2DF6F22D
#3      E 04/22/2013 17:07:02 3D9.1C2DDAC745
#4      E 04/22/2013 17:07:02 3D9.1C2DDAC745
#5      F 04/17/2013 15:14:39 3D9.1C2D1DB26E
#6      F 04/17/2013 15:14:43 3D9.1C2D1DB26E
#7      F 04/17/2013 15:14:43 3D9.1C2D1DB26E
#8      F 04/17/2013 15:14:43 3D9.1C2D1DB26E
于 2013-04-25T14:54:13.513 回答