Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/72.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';s_R_Csv_Dataframe_Split - Fatal编程技术网

R 通过检测将数据帧拆分为多个部分,然后写入多个csv';s

R 通过检测将数据帧拆分为多个部分,然后写入多个csv';s,r,csv,dataframe,split,R,Csv,Dataframe,Split,我有一个csv,如下图所示。数据是一组单独的表,由一个空行分隔,我要求这些表位于单独的csv文件中 导入到R之后,我想将数据拆分为不同的单独表,然后将这些表写入单独的csv文件。我想到了使用某种字符串检测,因为第一列中的第一个“Area”实例表示“new”表。有没有关于如何在R中处理此代码的想法?有许多表,手动执行此操作是不可取的 似乎还有一个截断问题,因为表需要有不同数量的列,但是我不认为消除NULL或NA数据会太困难 谢谢你的帮助 你应该把每一张不同的桌子放在最上面。总之,您有5个不同尺寸的

我有一个csv,如下图所示。数据是一组单独的表,由一个空行分隔,我要求这些表位于单独的csv文件中

导入到R之后,我想将数据拆分为不同的单独表,然后将这些表写入单独的csv文件。我想到了使用某种字符串检测,因为第一列中的第一个“Area”实例表示“new”表。有没有关于如何在R中处理此代码的想法?有许多表,手动执行此操作是不可取的

似乎还有一个截断问题,因为表需要有不同数量的列,但是我不认为消除NULL或NA数据会太困难

谢谢你的帮助


你应该把每一张不同的桌子放在最上面。总之,您有5个不同尺寸的表(表1:11x13;表2:11x9;表3:3x12;表4:10x5;表5:6x7)。将它们并排放在上面(A1:M11;N1:V11等)。表格的标题将在第一行

library(readxl)
# Use the path returned from getwd() function that is R's working directory
df <- as.data.frame(read_excel("C://Users//User//Documents//Revolution//Your.xlsx"))
库(readxl)
#使用从getwd()函数返回的路径,该函数是R的工作目录

df你应该把每个不同的表格放在最上面。总之,您有5个不同尺寸的表(表1:11x13;表2:11x9;表3:3x12;表4:10x5;表5:6x7)。将它们并排放在上面(A1:M11;N1:V11等)。表格的标题将在第一行

library(readxl)
# Use the path returned from getwd() function that is R's working directory
df <- as.data.frame(read_excel("C://Users//User//Documents//Revolution//Your.xlsx"))
库(readxl)
#使用从getwd()函数返回的路径,该函数是R的工作目录

df我不认为R是这种事情的正确工具。您应该始终根据任务使用正确的工具。由于已安装Excel,请运行此VBA脚本。那会做你想做的

Sub page_endings()
    Dim i As Long 'how many times for pagebreak
    Dim searchvalue_for_break_after 'value to do pagebreak
    searchvalue_for_break_after = ""
     'column A must be filled in with value break after
     'example row 6, 12, 18, 24 whatever row you want
     'will loop until empty row in column A
    For i = 1 To Range("A" & Rows.Count).End(xlUp).Row + 1
        If Range("A" & i).Value = searchvalue_for_break_after Then
             'will add a pagebreak after the row with value break after
            ActiveWindow.SelectedSheets.HPageBreaks.Add before:=Range("A" & i).Offset(1)
        End If
    Next i
    Call Create_Separate_Sheet_For_Each_HPageBreak
End Sub


Sub Create_Separate_Sheet_For_Each_HPageBreak()

    Dim HPB As HPageBreak
    Dim RW As Long
    Dim PageNum As Long
    Dim Asheet As Worksheet
    Dim Nsheet As Worksheet
    Dim Acell As Range

    'Sheet with the data, you can also use Sheets("Sheet1")
    Set Asheet = ActiveSheet

    If Asheet.HPageBreaks.Count = 0 Then
        MsgBox "There are no HPageBreaks"
        Exit Sub
    End If

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    'When the macro is ready we return to this cell on the ActiveSheet
    Set Acell = Range("A1")

    'Because of this bug we select a cell below your data
    'http://support.microsoft.com/default.aspx?scid=kb;en-us;210663
    Application.Goto Asheet.Range("A" & Rows.Count), True

    RW = 1
    PageNum = 1

    For Each HPB In Asheet.HPageBreaks
        'Add a sheet for the page
        With Asheet.Parent
            Set Nsheet = Worksheets.Add(after:=.Sheets(.Sheets.Count))
        End With

        'Give the sheet a name
        On Error Resume Next
        Nsheet.Name = "Page " & PageNum
        If Err.Number > 0 Then
            MsgBox "Change the name of : " & Nsheet.Name & " manually"
            Err.Clear
        End If
        On Error GoTo 0

        'Copy the cells from the page into the new sheet
        With Asheet
            .Range(.Cells(RW, "A"), .Cells(HPB.Location.Row - 1, "K")).Copy _
                    Nsheet.Cells(1)
        End With
        ' If you want to make values of your formulas use this line also
        ' Nsheet.UsedRange.Value = Nsheet.UsedRange.Value

        RW = HPB.Location.Row
        PageNum = PageNum + 1
    Next HPB

    Asheet.DisplayPageBreaks = False
    Application.Goto Acell, True

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
    Call SaveWorksheetsAsCsv
End Sub


Sub SaveWorksheetsAsCsv()

Dim WS As Excel.Worksheet
Dim SaveToDirectory As String

Dim CurrentWorkbook As String
Dim CurrentFormat As Long

CurrentWorkbook = ThisWorkbook.FullName
CurrentFormat = ThisWorkbook.FileFormat
' Store current details for the workbook
SaveToDirectory = "C:\Users\Excel\Desktop\"
For Each WS In ThisWorkbook.Worksheets
    Sheets(WS.Name).Copy
    ActiveWorkbook.SaveAs Filename:=SaveToDirectory & ThisWorkbook.Name & "-" & WS.Name & ".csv", FileFormat:=xlCSV
    ActiveWorkbook.Close savechanges:=False
    ThisWorkbook.Activate
Next

Application.DisplayAlerts = False
ThisWorkbook.SaveAs Filename:=CurrentWorkbook, FileFormat:=CurrentFormat
Application.DisplayAlerts = True
' Temporarily turn alerts off to prevent the user being prompted
'  about overwriting the original file.

End Sub

我不认为R是解决这类问题的合适工具。您应该始终根据任务使用正确的工具。由于已安装Excel,请运行此VBA脚本。那会做你想做的

Sub page_endings()
    Dim i As Long 'how many times for pagebreak
    Dim searchvalue_for_break_after 'value to do pagebreak
    searchvalue_for_break_after = ""
     'column A must be filled in with value break after
     'example row 6, 12, 18, 24 whatever row you want
     'will loop until empty row in column A
    For i = 1 To Range("A" & Rows.Count).End(xlUp).Row + 1
        If Range("A" & i).Value = searchvalue_for_break_after Then
             'will add a pagebreak after the row with value break after
            ActiveWindow.SelectedSheets.HPageBreaks.Add before:=Range("A" & i).Offset(1)
        End If
    Next i
    Call Create_Separate_Sheet_For_Each_HPageBreak
End Sub


Sub Create_Separate_Sheet_For_Each_HPageBreak()

    Dim HPB As HPageBreak
    Dim RW As Long
    Dim PageNum As Long
    Dim Asheet As Worksheet
    Dim Nsheet As Worksheet
    Dim Acell As Range

    'Sheet with the data, you can also use Sheets("Sheet1")
    Set Asheet = ActiveSheet

    If Asheet.HPageBreaks.Count = 0 Then
        MsgBox "There are no HPageBreaks"
        Exit Sub
    End If

    With Application
        .ScreenUpdating = False
        .EnableEvents = False
    End With

    'When the macro is ready we return to this cell on the ActiveSheet
    Set Acell = Range("A1")

    'Because of this bug we select a cell below your data
    'http://support.microsoft.com/default.aspx?scid=kb;en-us;210663
    Application.Goto Asheet.Range("A" & Rows.Count), True

    RW = 1
    PageNum = 1

    For Each HPB In Asheet.HPageBreaks
        'Add a sheet for the page
        With Asheet.Parent
            Set Nsheet = Worksheets.Add(after:=.Sheets(.Sheets.Count))
        End With

        'Give the sheet a name
        On Error Resume Next
        Nsheet.Name = "Page " & PageNum
        If Err.Number > 0 Then
            MsgBox "Change the name of : " & Nsheet.Name & " manually"
            Err.Clear
        End If
        On Error GoTo 0

        'Copy the cells from the page into the new sheet
        With Asheet
            .Range(.Cells(RW, "A"), .Cells(HPB.Location.Row - 1, "K")).Copy _
                    Nsheet.Cells(1)
        End With
        ' If you want to make values of your formulas use this line also
        ' Nsheet.UsedRange.Value = Nsheet.UsedRange.Value

        RW = HPB.Location.Row
        PageNum = PageNum + 1
    Next HPB

    Asheet.DisplayPageBreaks = False
    Application.Goto Acell, True

    With Application
        .ScreenUpdating = True
        .EnableEvents = True
    End With
    Call SaveWorksheetsAsCsv
End Sub


Sub SaveWorksheetsAsCsv()

Dim WS As Excel.Worksheet
Dim SaveToDirectory As String

Dim CurrentWorkbook As String
Dim CurrentFormat As Long

CurrentWorkbook = ThisWorkbook.FullName
CurrentFormat = ThisWorkbook.FileFormat
' Store current details for the workbook
SaveToDirectory = "C:\Users\Excel\Desktop\"
For Each WS In ThisWorkbook.Worksheets
    Sheets(WS.Name).Copy
    ActiveWorkbook.SaveAs Filename:=SaveToDirectory & ThisWorkbook.Name & "-" & WS.Name & ".csv", FileFormat:=xlCSV
    ActiveWorkbook.Close savechanges:=False
    ThisWorkbook.Activate
Next

Application.DisplayAlerts = False
ThisWorkbook.SaveAs Filename:=CurrentWorkbook, FileFormat:=CurrentFormat
Application.DisplayAlerts = True
' Temporarily turn alerts off to prevent the user being prompted
'  about overwriting the original file.

End Sub

仍然不清楚在什么基础上split@joel.wilson我想拆分图像中的不同表。您能以csv格式共享该示例吗?我有一个类似的问题,只是我想要一个结构化表。看看,
findTable
函数,也许你可以将解决方案改编成类似seek和extract的东西,然后迭代这个过程,直到你有了所有的表格。把你的.xlsx文件放到网络上,这样我也可以用它来做。仍然不清楚使用它的基础split@joel.wilson我想拆分图像中的不同表。您能以csv格式共享该示例吗?我有一个类似的问题,只是我想要一个结构化表。看看,
findTable
函数,也许你可以将解决方案改编成类似seek和extract的东西,然后迭代这个过程,直到你拥有所有的表。将你的.xlsx文件放到web上,这样我也可以使用它。