下面是我用来读取xml文件并使用xml文件中的属性创建数据框的代码。但是,我对代码的问题很小。 Here是数据的示例。
当我将名为attribact
的第一个列表转换为名为k1
的数据帧时,列表中名为x
的列将更改为k1
。代码data1 <- cbind(k1,k2)
的最后一行也发生了同样的事情。这里,列名k1
(来自前面的步骤)更改为data1
换句话说,列表中的一个列名称正被分配给列表的对象名称替换。< / p>
plans <- xmlParse(file1)
idNodes <- getNodeSet(plans, "//person[@id]")
ids <- lapply(idNodes, function(x) xmlAttrs(x)['id'])
attribact <- vector("list", length(ids)*14)
k <- 1
for (i in 1:length(ids))
{
act <- xpathApply(idNodes[[i]], path = paste("//person[@id=", as.numeric(ids[[i]]),"]//plan[@selected='yes']//act|leg", sep=""), xmlAttrs)
for (j in 1:length(act))
{
attribact[[k]] <- c(act[[j]],ids[[i]])
k <- k+1
}
}
attribact <- attribact[attribact!='NULL']
k1 <- t(as.data.frame(attribact))
data1 <- k1
答案 0 :(得分:1)
我认为有更好的方法可以做到这一点,但为了给你一个快速的答案,应该重新使用你的代码,juste改变结束
您的代码
library(XML)
plans <- xmlParse(file1)
idNodes <- getNodeSet(plans, "//person[@id]")
ids <- lapply(idNodes, function(x) xmlAttrs(x)['id'])
attribact <- vector("list", length(ids)*14)
k <- 1
for (i in 1:length(ids))
{
act <- xpathApply(idNodes[[i]], path = paste("//person[@id=", as.numeric(ids[[i]]),"]//plan[@selected='yes']//act|leg", sep=""), xmlAttrs)
for (j in 1:length(act))
{
attribact[[k]] <- c(act[[j]],ids[[i]])
k <- k+1
}
}
attribact <- attribact[attribact!='NULL']
然后执行
data <- t(do.call(cbind, attribact))
这是你想要的吗?
head(data)
#> type link x y start_time end_time
#> [1,] "home" "21258" "334867.243653" "3126570.70778" "03:00:00" "15:07:00"
#> [2,] "shop" "13904" "332634.86999" "3127078.96383" "15:12:00" "16:21:00"
#> [3,] "shop" "14129" "331666.364904" "3129306.48785" "16:25:00" "17:37:00"
#> [4,] "home" "21258" "334867.243653" "3126570.70778" "17:45:00" "26:59:00"
#> [5,] "home" "21256" "334598.361546" "3126269.05167" "03:00:00" "07:56:00"
#> [6,] "work" "14057" "335957.065395" "3128105.16619" "08:04:00" "10:28:00"
#> id
#> [1,] "10000061"
#> [2,] "10000061"
#> [3,] "10000061"
#> [4,] "10000061"
#> [5,] "10000302"
#> [6,] "10000302"