我对R编程有疑问。
我有一个示例代码。这是
x <- read.table("example.txt", sep="=", header=F, nrows=6, fill=T)
x <-x[,c(1:2)]
x_t <- as.data.frame(t(x1))
names(x_t) <- c("ID", "URL", "SIZE", "DATE", "TIME", "DATASET")
x_t <- x_t[c(2),]
rownames(x_t) <- NULL
所以,我更改了源代码。我的源代码是,
file <- list.files("./dataSet")
for(i in 1:length(file)){
location[i] = paste("./dataSet/", file[i], sep="")
}
for(i in 1:1000){
assign(paste("a", i, sep=""), read.table(location[i], sep="=", header=F, nrow=6, fill=TRUE))
assign(paste("a", i), paste("a", i, "[,c(1:2)]"))
**assign(paste("a", i, "_t", sep=""), as.data.frame(t(paste("a", i, sep=""))))**
assign(paste("a", i, "_t"), paste("a", i, "[c(2),]"))
}
a1的结果是:
a1_t的结果是:
但是,例子的结果是:
我想用示例代码获得相同的结果。
我认为“** **”的一部分是问题。
如果我添加
assign(rownames(paste("a", i, "_t")), NULL)
我该如何编写源代码?
如果你知道解决方案,请帮帮我。 谢谢。
答案 0 :(得分:0)
data.frame(
V1 = c("ID", "URL", "SIZE", "DATE", "TIME", "DATASET"),
V2 = c("A0001", "htttp://long.url", "54258", "27/06/02", "16:47:17", "Commercial Banks"),
stringsAsFactors=FALSE # use stringsAsFactors = FALSE in your `read.table()` call as well
) -> xdf
xdf
## V1 V2
## 1 ID A0001
## 2 URL htttp://long.url
## 3 SIZE 54258
## 4 DATE 27/06/02
## 5 TIME 16:47:17
## 6 DATASET Commercial Banks
docxtractr::assign_colnames(
as.data.frame(
t(xdf),
stringsAsFactors=FALSE
),
1)
## ID URL SIZE DATE TIME DATASET
## 1 A0001 htttp://long.url 54258 27/06/02 16:47:17 Commercial Banks