R 从.csv中提取列数据并将每10个连续行转换为相应的列
下面是我试图实现的代码。我想提取这10个连续的行值,并将它们转换为相应的列 以下是数据的外观: 我一直在尝试,但是R 从.csv中提取列数据并将每10个连续行转换为相应的列,r,csv,data-cleaning,R,Csv,Data Cleaning,下面是我试图实现的代码。我想提取这10个连续的行值,并将它们转换为相应的列 以下是数据的外观: 我一直在尝试,但是temp1和temp2结果是空的。请帮忙 library(Hmisc) #for increment function myData <- read.csv("Clothing_&_Accessories.csv",header=FALSE,sep=",",fill=TRUE) # reading the csv file extract<-myDat
temp1
和temp2
结果是空的。请帮忙
library(Hmisc) #for increment function
myData <- read.csv("Clothing_&_Accessories.csv",header=FALSE,sep=",",fill=TRUE) # reading the csv file
extract<-myData$V2 # extracting the desired column
x<-1
y<-1
temp1 <- NULL #initialisation
temp2 <- NULL #initialisation
data.sorted <- NULL #initialisation
limit<-nrow(myData) # Calculating no of rows
while (x! = limit) {
count <- 1
for (count in 11) {
if (count > 10) {
inc(x) <- 1
break # gets out of for loop
}
else {
temp1[y]<-data_mat[x] # extracting by every row element
}
inc(x) <- 1 # increment x
inc(y) <- 1 # increment y
}
temp2<-temp1
data.sorted<-rbind(data.sorted,temp2) # turn rows into columns
}
library(Hmisc)#用于增量函数
myData您的代码太复杂了。您可以只使用一个for循环完成此操作,而不使用外部包,如下所示:
myData <- as.data.frame(matrix(c(rep("a", 10), "", rep("b", 10)), ncol=1), stringsAsFactors = FALSE)
newData <- data.frame(row.names=1:10)
for (i in 1:((nrow(myData)+1)/11)) {
start <- 11*i - 10
newData[[paste0("col", i)]] <- myData$V1[start:(start+9)]
}
myData您的代码太复杂了。您可以只使用一个for循环完成此操作,而不使用外部包,如下所示:
myData <- as.data.frame(matrix(c(rep("a", 10), "", rep("b", 10)), ncol=1), stringsAsFactors = FALSE)
newData <- data.frame(row.names=1:10)
for (i in 1:((nrow(myData)+1)/11)) {
start <- 11*i - 10
newData[[paste0("col", i)]] <- myData$V1[start:(start+9)]
}
myData我们可以基于“V2”列中的'
值创建数字索引,split
数据集,使用Reduce/merge
以宽格式获取列
indx <- cumsum(myData$V2=='')+1
res <- Reduce(function(...) merge(..., by= 'V1'), split(myData, indx))
res1 <- res[order(factor(res$V1, levels=myData[1:10, 1])),]
colnames(res1)[-1] <- paste0('Col', 1:3)
head(res1,3)
# V1 Col1 Col2 Col3
#2 ProductId B000179R3I B0000C3XXN B0000C3XX9
#4 product_title Amazon.com Amazon.com Amazon.com
#3 product_price unknown unknown unknown
我想这只是一个重塑的问题。在这种情况下,我们可以使用dcast
fromdata.table
将long
格式转换为wide
格式
library(data.table)
DT <- dcast(setDT(myData)[V1!=''][, N:= paste0('Col', 1:.N) ,V1], V1~N,
value.var='V2')
库(data.table)
DT我们可以基于“V2”列中的'
值创建一个数字索引,split
数据集,使用Reduce/merge
获得宽格式的列
indx <- cumsum(myData$V2=='')+1
res <- Reduce(function(...) merge(..., by= 'V1'), split(myData, indx))
res1 <- res[order(factor(res$V1, levels=myData[1:10, 1])),]
colnames(res1)[-1] <- paste0('Col', 1:3)
head(res1,3)
# V1 Col1 Col2 Col3
#2 ProductId B000179R3I B0000C3XXN B0000C3XX9
#4 product_title Amazon.com Amazon.com Amazon.com
#3 product_price unknown unknown unknown
我想这只是一个重塑的问题。在这种情况下,我们可以使用dcast
fromdata.table
将long
格式转换为wide
格式
library(data.table)
DT <- dcast(setDT(myData)[V1!=''][, N:= paste0('Col', 1:.N) ,V1], V1~N,
value.var='V2')
库(data.table)
DT当数据集太大(如1048576个条目)时该怎么办,因为它花费了太长的时间。重塑的方法解决了这个问题,谢谢。实际上,我想对从该数据集中提取的评论进行情感分析,所以我想先重新排列并清理它。@VarunKhambra是的,merge/Reduce
对于大型数据集,data.table
dcast
应该很快。当数据集太大(如1048576个条目)时该怎么办因为它花费了太长的时间。重塑的事情解决了它,谢谢。实际上,我想对从这个数据集中提取的评论进行情感分析,所以我想先重新排列和清理它。@VarunKhambra是的,merge/Reduce
对于大型数据集,data.table
dcast
应该很快。