R能读取不规则的xlsx吗?

R能读取不规则的xlsx吗?,r,r-xlsx,R,R Xlsx,我有这么多(约1000)xlsx像上面的图片。我想读每一个xlsx,得到每个坎达塔特人的姓名、号码和年龄的数据。但是我不知道如何阅读这个不规则的xlsx?我不知道是否有任何R Excel API足够聪明来处理列格式,但是有一个简单的解决方法。您只需以CSV格式保存上述工作表即可。对上面显示的数据执行此操作后,我有以下三行CSV代码: Title,,,,, name,mike,number,123214,age,28 ,score,,ddd,aaa,bbb 您可以尝试以下代码: df <-


我有这么多(约1000)xlsx像上面的图片。我想读每一个xlsx,得到每个坎达塔特人的姓名、号码和年龄的数据。但是我不知道如何阅读这个不规则的xlsx?

我不知道是否有任何R Excel API足够聪明来处理列格式,但是有一个简单的解决方法。您只需以CSV格式保存上述工作表即可。对上面显示的数据执行此操作后,我有以下三行CSV代码:

Title,,,,,
name,mike,number,123214,age,28
,score,,ddd,aaa,bbb
您可以尝试以下代码:

df <- read.csv(file="path/to/your/file.csv", header=FALSE)
df <- df[2:nrow(df), ]      # drop first row

df我不知道是否有任何R Excel API足够聪明来处理列格式,但有一个简单的解决方法。您只需以CSV格式保存上述工作表即可。对上面显示的数据执行此操作后,我有以下三行CSV代码:

Title,,,,,
name,mike,number,123214,age,28
,score,,ddd,aaa,bbb
您可以尝试以下代码:

df <- read.csv(file="path/to/your/file.csv", header=FALSE)
df <- df[2:nrow(df), ]      # drop first row

df您可以在Excel中运行VBA代码,并将任意数量的XLSX文件转换为CSV文件。然后循环遍历所有CSV文件,将所有CSV文件合并到R中的数据帧中

Sub Convert_Excel_To_CSV()
    Dim MyPath As String, FilesInPath As String
    Dim MyFiles() As String, Fnum As Long
    Dim mybook As Workbook
    Dim CalcMode As Long
    Dim sh As Worksheet
    Dim ErrorYes As Boolean
    Dim LPosition As Integer

    'Fill in the path\folder where the Excel files are
    MyPath = "C:\Users\Ryan\Desktop\Excel_Files\"

    FilesInPath = Dir(MyPath & "*.xlsx*")
    If FilesInPath = "" Then
        MsgBox "No files found"
        Exit Sub
    End If

    Fnum = 0
    Do While FilesInPath <> ""
        Fnum = Fnum + 1
        ReDim Preserve MyFiles(1 To Fnum)
        MyFiles(Fnum) = FilesInPath
        FilesInPath = Dir()
    Loop

    With Application
        CalcMode = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    If Fnum > 0 Then
        For Fnum = LBound(MyFiles) To UBound(MyFiles)
            Set mybook = Nothing
            On Error Resume Next
            Set mybook = Workbooks.Open(MyPath & MyFiles(Fnum))
            On Error GoTo 0

            If Not mybook Is Nothing Then


                    LPosition = InStr(1, mybook.Name, ".") - 1
                    mybookname = Left(mybook.Name, LPosition)
                    mybook.Activate
                    'All XLSX Files get saved in the directory below:
                    ActiveWorkbook.SaveAs Filename:="C:\your_path_here\" & mybookname & ".csv" _
                        , FileFormat:=xlCSVMSDOS, _
                        CreateBackup:=False

            End If

            mybook.Close SaveChanges:=False

        Next Fnum
    End If

    If ErrorYes = True Then
        MsgBox "There are problems in one or more files, possible problem:" _
             & vbNewLine & "protected workbook/sheet or a sheet/range that not exist"
    End If

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
        .Calculation = CalcMode
    End With
End Sub






setwd("C:/your_path")
fnames <- list.files()
csv <- lapply(fnames, read.csv)
result <- do.call(rbind, csv)


********  ********  ********  ********  ********  ********  ********  ********  


filedir <- setwd("C:/your_path")
file_names <- dir(filedir)
your_data_frame <- do.call(rbind,lapply(file_names,read.csv))


********  ********  ********  ********  ********  ********  ********  ********  


filedir <- setwd("C:/your_path")
file_names <- dir(filedir)
your_data_frame <- do.call(rbind, lapply(file_names, read.csv, skip = 1, header = FALSE))


********  ********  ********  ********  ********  ********  ********  ********  


filedir <- setwd("C:/your_path")
file_names <- dir(filedir)
your_data_frame <- do.call(rbind, lapply(file_names, read.csv, header = FALSE))


********  ********  ********  ********  ********  ********  ********  ********  


# 
temp <- setwd("C:/your_path")
temp = list.files(pattern="*.csv")
myfiles = lapply(temp, read.delim)
将\u Excel\u子转换为\u CSV()
将MyPath设置为字符串,将FileInPath设置为字符串
Dim MyFiles()作为字符串,Fnum作为长度
将mybook设置为工作簿
暗淡的CalcMode与长
将sh设置为工作表
Dim ErrorYes作为布尔值
作为整数的Dim L位置
'填写Excel文件所在的路径\文件夹
MyPath=“C:\Users\Ryan\Desktop\Excel\u文件”
FilesInPath=Dir(MyPath&“*.xlsx*”)
如果FilesInPath=“”,则
MsgBox“未找到任何文件”
出口接头
如果结束
Fnum=0
在文件输入路径“”时执行此操作
Fnum=Fnum+1
ReDim保留我的文件(1到Fnum)
MyFiles(Fnum)=FilesInPath
FilesInPath=Dir()
环
应用
CalcMode=.Calculation
.Calculation=xlCalculationManual
.ScreenUpdate=False
.EnableEvents=False
以
如果Fnum>0,则
对于Fnum=LBound(MyFiles)到UBound(MyFiles)
设置mybook=Nothing
出错时继续下一步
设置mybook=Workbooks.Open(MyPath&MyFiles(Fnum))
错误转到0
如果不是的话,我的书什么都不是
LPosition=InStr(1,mybook.Name,“.”-1
mybookname=左侧(mybook.Name,LPosition)
我的书,激活
'所有XLSX文件都保存在以下目录中:
ActiveWorkbook.SaveAs文件名:=“C:\your\u path\u here\”&mybookname&“.csv”_
,文件格式:=xlCSVMSDOS_
CreateBackup:=False
如果结束
mybook.Close SaveChanges:=False
下一个Fnum
如果结束
如果ErrorYes=True,则
MsgBox“一个或多个文件中存在问题,可能存在问题:”_
&vbNewLine&“受保护的工作簿/工作表或不存在的工作表/范围”
如果结束
应用
.ScreenUpdate=True
.EnableEvents=True
.Calculation=CalcMode
以
端接头
setwd(“C:/your_路径”)

fnames您可以在Excel中运行VBA代码,并将任意数量的XLSX文件转换为CSV文件。然后循环遍历所有CSV文件,将所有CSV文件合并到R中的数据帧中

Sub Convert_Excel_To_CSV()
    Dim MyPath As String, FilesInPath As String
    Dim MyFiles() As String, Fnum As Long
    Dim mybook As Workbook
    Dim CalcMode As Long
    Dim sh As Worksheet
    Dim ErrorYes As Boolean
    Dim LPosition As Integer

    'Fill in the path\folder where the Excel files are
    MyPath = "C:\Users\Ryan\Desktop\Excel_Files\"

    FilesInPath = Dir(MyPath & "*.xlsx*")
    If FilesInPath = "" Then
        MsgBox "No files found"
        Exit Sub
    End If

    Fnum = 0
    Do While FilesInPath <> ""
        Fnum = Fnum + 1
        ReDim Preserve MyFiles(1 To Fnum)
        MyFiles(Fnum) = FilesInPath
        FilesInPath = Dir()
    Loop

    With Application
        CalcMode = .Calculation
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    If Fnum > 0 Then
        For Fnum = LBound(MyFiles) To UBound(MyFiles)
            Set mybook = Nothing
            On Error Resume Next
            Set mybook = Workbooks.Open(MyPath & MyFiles(Fnum))
            On Error GoTo 0

            If Not mybook Is Nothing Then


                    LPosition = InStr(1, mybook.Name, ".") - 1
                    mybookname = Left(mybook.Name, LPosition)
                    mybook.Activate
                    'All XLSX Files get saved in the directory below:
                    ActiveWorkbook.SaveAs Filename:="C:\your_path_here\" & mybookname & ".csv" _
                        , FileFormat:=xlCSVMSDOS, _
                        CreateBackup:=False

            End If

            mybook.Close SaveChanges:=False

        Next Fnum
    End If

    If ErrorYes = True Then
        MsgBox "There are problems in one or more files, possible problem:" _
             & vbNewLine & "protected workbook/sheet or a sheet/range that not exist"
    End If

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
        .Calculation = CalcMode
    End With
End Sub






setwd("C:/your_path")
fnames <- list.files()
csv <- lapply(fnames, read.csv)
result <- do.call(rbind, csv)


********  ********  ********  ********  ********  ********  ********  ********  


filedir <- setwd("C:/your_path")
file_names <- dir(filedir)
your_data_frame <- do.call(rbind,lapply(file_names,read.csv))


********  ********  ********  ********  ********  ********  ********  ********  


filedir <- setwd("C:/your_path")
file_names <- dir(filedir)
your_data_frame <- do.call(rbind, lapply(file_names, read.csv, skip = 1, header = FALSE))


********  ********  ********  ********  ********  ********  ********  ********  


filedir <- setwd("C:/your_path")
file_names <- dir(filedir)
your_data_frame <- do.call(rbind, lapply(file_names, read.csv, header = FALSE))


********  ********  ********  ********  ********  ********  ********  ********  


# 
temp <- setwd("C:/your_path")
temp = list.files(pattern="*.csv")
myfiles = lapply(temp, read.delim)
将\u Excel\u子转换为\u CSV()
将MyPath设置为字符串,将FileInPath设置为字符串
Dim MyFiles()作为字符串,Fnum作为长度
将mybook设置为工作簿
暗淡的CalcMode与长
将sh设置为工作表
Dim ErrorYes作为布尔值
作为整数的Dim L位置
'填写Excel文件所在的路径\文件夹
MyPath=“C:\Users\Ryan\Desktop\Excel\u文件”
FilesInPath=Dir(MyPath&“*.xlsx*”)
如果FilesInPath=“”,则
MsgBox“未找到任何文件”
出口接头
如果结束
Fnum=0
在文件输入路径“”时执行此操作
Fnum=Fnum+1
ReDim保留我的文件(1到Fnum)
MyFiles(Fnum)=FilesInPath
FilesInPath=Dir()
环
应用
CalcMode=.Calculation
.Calculation=xlCalculationManual
.ScreenUpdate=False
.EnableEvents=False
以
如果Fnum>0,则
对于Fnum=LBound(MyFiles)到UBound(MyFiles)
设置mybook=Nothing
出错时继续下一步
设置mybook=Workbooks.Open(MyPath&MyFiles(Fnum))
错误转到0
如果不是的话,我的书什么都不是
LPosition=InStr(1,mybook.Name,“.”-1
mybookname=左侧(mybook.Name,LPosition)
我的书,激活
'所有XLSX文件都保存在以下目录中:
ActiveWorkbook.SaveAs文件名:=“C:\your\u path\u here\”&mybookname&“.csv”_
,文件格式:=xlCSVMSDOS_
CreateBackup:=False
如果结束
mybook.Close SaveChanges:=False
下一个Fnum
如果结束
如果ErrorYes=True,则
MsgBox“一个或多个文件中存在问题,可能存在问题:”_
&vbNewLine&“受保护的工作簿/工作表或不存在的工作表/范围”
如果结束
应用
.ScreenUpdate=True
.EnableEvents=True
.Calculation=CalcMode
以
端接头
setwd(“C:/your_路径”)

fnames那么你想要每个xlsx文件的第一行吗?@AndreElrico,嗯,是的,我不能用read.xlsx来读取这种xlsx,它会返回一些错误。但是read.csv可以做到这一点我不知道为什么你想要每个xlsx文件的第一行?@AndreElrico,是的,我不能用read.xlsx来读取这种xlsx,它会返回一些错误。但是read.csv可以做到这一点我不知道为什么要感谢你。实际上我使用read.xlsx来读取这些xlsx,但是失败了,但是现在我发现read.csv可以处理这个问题,谢谢。实际上我使用read.xlsx来读取这些xlsx,但是失败了,但是现在我发现read.csv可以处理这个问题