R - 函数t()在as.data.frame()中[关闭]

问题描述 投票:-4回答:1

我对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) 

我该如何编写源代码?

如果你知道解决方案,请帮助我。谢谢。

r dataframe
1个回答
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
© www.soinside.com 2019 - 2024. All rights reserved.