Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/82.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 从.csv中提取列数据并将每10个连续行转换为相应的列_R_Csv_Data Cleaning - Fatal编程技术网

R 从.csv中提取列数据并将每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

下面是我试图实现的代码。我想提取这10个连续的行值,并将它们转换为相应的列

以下是数据的外观:

我一直在尝试,但是
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
from
data.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
from
data.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
应该很快。