How to Read First Few Lines of a File in R

read_lines() reads up to n_max lines from a file. New lines are not included in the output. read_lines_raw() produces a list of raw vectors, and is useful for handling information with unknown encoding. write_lines() takes a graphic symbol vector or listing of raw vectors, appending a new line after each entry.

Usage

                              read_lines                (                file,   skip                =                0,   skip_empty_rows                =                Simulated,   n_max                =                Inf,   locale                =                default_locale                (                ),   na                =                character                (                ),   lazy                =                should_read_lazy                (                ),   num_threads                =                readr_threads                (                ),   progress                =                show_progress                (                )                )                read_lines_raw                (                file,   skip                =                0,   n_max                =                -                1L,   num_threads                =                readr_threads                (                ),   progress                =                show_progress                (                )                )                write_lines                (                10,                file,   sep                =                "\due north",   na                =                "NA",   append                =                FALSE,   num_threads                =                readr_threads                (                ),   path                =                deprecated                (                )                )                          

Arguments

file

Either a path to a file, a connection, or literal data (either a unmarried string or a raw vector).

Files ending in .gz, .bz2, .xz, or .nil will be automatically uncompressed. Files starting with http://, https://, ftp://, or ftps:// will exist automatically downloaded. Remote gz files can besides be automatically downloaded and decompressed.

Literal data is nearly useful for examples and tests. To be recognised as literal data, the input must be either wrapped with I(), be a string containing at least 1 new line, or be a vector containing at least one string with a new line.

Using a value of clipboard() will read from the system clipboard.

skip

Number of lines to skip before reading data.

skip_empty_rows

Should bare rows be ignored birthday? i.e. If this option is Truthful then blank rows will non be represented at all. If information technology is Faux and then they will exist represented by NA values in all the columns.

n_max

Number of lines to read. If n_max is -1, all lines in file volition be read.

locale

The locale controls defaults that vary from place to place. The default locale is US-centric (similar R), but yous can utilise locale() to create your own locale that controls things like the default time zone, encoding, decimal mark, large mark, and day/month names.

na

Character vector of strings to interpret as missing values. Set this option to graphic symbol() to indicate no missing values.

lazy

Read values lazily? By default the file is initially just indexed and the values are read lazily when accessed. Lazy reading is useful interactively, specially if yous are only interested in a subset of the full dataset. Annotation, if you later write to the same file you read from y'all need to fix lazy = Simulated. On Windows the file will exist locked and on other systems the memory map will get invalid.

num_threads

The number of processing threads to utilise for initial parsing and lazy reading of data. If your information contains newlines within fields the parser should automatically find this and fall back to using one thread but. However if you know your file has newlines within quoted fields information technology is safest to prepare num_threads = 1 explicitly.

progress

Brandish a progress bar? By default it will just display in an interactive session and non while knitting a document. The automatic progress bar can be disabled by setting option readr.show_progress to Fake.

10

A character vector or listing of raw vectors to write to disk.

sep

The line separator. Defaults to \\n, ordinarily used on POSIX systems similar macOS and linux. For native windows (CRLF) separators use \\r\\n.

append

If FALSE, volition overwrite existing file. If TRUE, will append to existing file. In both cases, if the file does not exist a new file is created.

path

[Deprecated] Use the file argument instead.

Value

read_lines(): A character vector with one element for each line. read_lines_raw(): A listing containing a raw vector for each line. write_lines() returns x, invisibly.

Examples

                                                read_lines                  (                  file.path                  (                  R.home                  (                  "doc"                  ),                  "AUTHORS"                  ), n_max                  =                  ten                  )                                                  #>                  [1] "Authors of R."                                                  #>                  [2] ""                                                  #>                  [3] "R was initially written by Robert Admirer and Ross Ihaka—also known as \"R & R\""                                  #>                  [iv] "of the Statistics Department of the University of Auckland."                                                  #>                  [5] ""                                                  #>                  [6] "Since mid-1997 there has been a cadre group with write access to the R"                                                  #>                  [7] "source, currently consisting of"                                                  #>                  [8] ""                                                  #>                  [9] "Douglas Bates"                                                  #>                  [10] "John Chambers"                                                  read_lines_raw                  (                  file.path                  (                  R.home                  (                  "doc"                  ),                  "AUTHORS"                  ), n_max                  =                  x                  )                                                  #>                  [[1]]                                  #>                  [1] 41 75 74 68 6f 72 73 20 6f 66 20 52 2e                                  #>                                                  #>                  [[ii]]                                  #>                  raw(0)                                  #>                                                  #>                  [[3]]                                  #>                  [ane] 52 20 77 61 73 20 69 6e 69 74 69 61 6c 6c 79 xx 77 72 69 74 74 65 6e                                  #>                  [24] twenty 62 79 20 52 6f 62 65 72 74 twenty 47 65 6e 74 6c 65 6d 61 6e 20 61 6e                                  #>                  [47] 64 20 52 6f 73 73 20 49 68 61 6b 61 e2 80 94 61 6c 73 6f 20 6b 6e 6f                                  #>                  [70] 77 6e xx 61 73 20 22 52 20 26 20 52 22                                  #>                                                  #>                  [[4]]                                  #>                  [1] 6f 66 20 74 68 65 xx 53 74 61 74 69 73 74 69 63 73 20 44 65 seventy 61 72                                  #>                  [24] 74 6d 65 6e 74 20 6f 66 twenty 74 68 65 20 55 6e 69 76 65 72 73 69 74 79                                  #>                  [47] 20 6f 66 twenty 41 75 63 6b 6c 61 6e 64 2e                                  #>                                                  #>                  [[5]]                                  #>                  raw(0)                                  #>                                                  #>                  [[half-dozen]]                                  #>                  [1] 53 69 6e 63 65 twenty 6d 69 64 2d 31 39 39 37 20 74 68 65 72 65 20 68 61                                  #>                  [24] 73 20 62 65 65 6e xx 61 xx 63 6f 72 65 20 67 72 6f 75 70 20 77 69 74                                  #>                  [47] 68 20 77 72 69 74 65 xx 61 63 63 65 73 73 20 74 6f 20 74 68 65 20 52                                  #>                                                  #>                  [[7]]                                  #>                  [1] 73 6f 75 72 63 65 2c xx 63 75 72 72 65 6e 74 6c 79 20 63 6f 6e 73 69                                  #>                  [24] 73 74 69 6e 67 20 6f 66                                  #>                                                  #>                  [[viii]]                                  #>                  raw(0)                                  #>                                                  #>                  [[9]]                                  #>                  [1] 44 6f 75 67 6c 61 73 xx 42 61 74 65 73                                  #>                                                  #>                  [[ten]]                                  #>                  [ane] 4a 6f 68 6e 20 43 68 61 6d 62 65 72 73                                  #>                                                                  tmp                  <-                  tempfile                  (                  )                                                                  write_lines                  (                  rownames                  (                  mtcars                  ),                  tmp                  )                                                  read_lines                  (                  tmp, lazy                  =                  FALSE                  )                                                  #>                  [1] "Mazda RX4"           "Mazda RX4 Wag"       "Datsun 710"                                                  #>                  [4] "Hornet 4 Drive"      "Hornet Sportabout"   "Valiant"                                                  #>                  [7] "Duster 360"          "Merc 240D"           "Merc 230"                                                  #>                  [10] "Merc 280"            "Merc 280C"           "Merc 450SE"                                                  #>                  [13] "Merc 450SL"          "Merc 450SLC"         "Cadillac Fleetwood"                                                  #>                  [16] "Lincoln Continental" "Chrysler Imperial"   "Fiat 128"                                                  #>                  [19] "Honda Civic"         "Toyota Corolla"      "Toyota Corona"                                                  #>                  [22] "Dodge Challenger"    "AMC Javelin"         "Camaro Z28"                                                  #>                  [25] "Pontiac Firebird"    "Fiat X1-9"           "Porsche 914-ii"                                                  #>                  [28] "Lotus Europa"        "Ford Pantera L"      "Ferrari Dino"                                                  #>                  [31] "Maserati Bora"       "Volvo 142E"                                                  read_file                  (                  tmp                  )                  # annotation trailing \n                                                  #>                  [ane] "Mazda RX4\nMazda RX4 Wag\nDatsun 710\nHornet iv Drive\nHornet Sportabout\nValiant\nDuster 360\nMerc 240D\nMerc 230\nMerc 280\nMerc 280C\nMerc 450SE\nMerc 450SL\nMerc 450SLC\nCadillac Fleetwood\nLincoln Continental\nChrysler Royal\nFiat 128\nHonda Borough\nToyota Corolla\nToyota Corona\nDodge Challenger\nAMC Javelin\nCamaro Z28\nPontiac Firebird\nFiat X1-ix\nPorsche 914-2\nLotus Europa\nFord Pantera L\nFerrari Dino\nMaserati Bora\nVolvo 142E\n"                                                  write_lines                  (                  airquality                  $                  Ozone,                  tmp, na                  =                  "-1"                  )                                                  read_lines                  (                  tmp                  )                                                  #>                  [1] "41"  "36"  "12"  "18"  "-1"  "28"  "23"  "19"  "8"   "-ane"  "7"                                                  #>                  [12] "16"  "11"  "fourteen"  "18"  "14"  "34"  "6"   "thirty"  "11"  "one"   "xi"                                                  #>                  [23] "4"   "32"  "-1"  "-1"  "-1"  "23"  "45"  "115" "37"  "-ane"  "-1"                                                  #>                  [34] "-i"  "-1"  "-1"  "-1"  "29"  "-i"  "71"  "39"  "-1"  "-1"  "23"                                                  #>                  [45] "-1"  "-1"  "21"  "37"  "20"  "12"  "xiii"  "-1"  "-1"  "-i"  "-1"                                                  #>                  [56] "-1"  "-1"  "-ane"  "-one"  "-1"  "-1"  "135" "49"  "32"  "-one"  "64"                                                  #>                  [67] "40"  "77"  "97"  "97"  "85"  "-i"  "x"  "27"  "-1"  "vii"   "48"                                                  #>                  [78] "35"  "61"  "79"  "63"  "16"  "-i"  "-ane"  "lxxx"  "108" "xx"  "52"                                                  #>                  [89] "82"  "50"  "64"  "59"  "39"  "9"   "16"  "78"  "35"  "66"  "122"                                  #>                  [100] "89"  "110" "-1"  "-1"  "44"  "28"  "65"  "-1"  "22"  "59"  "23"                                                  #>                  [111] "31"  "44"  "21"  "9"   "-1"  "45"  "168" "73"  "-i"  "76"  "118"                                  #>                  [122] "84"  "85"  "96"  "78"  "73"  "91"  "47"  "32"  "20"  "23"  "21"                                                  #>                  [133] "24"  "44"  "21"  "28"  "9"   "13"  "46"  "18"  "thirteen"  "24"  "16"                                                  #>                  [144] "thirteen"  "23"  "36"  "7"   "14"  "xxx"  "-1"  "14"  "eighteen"  "20"                                          

roundscoon1999.blogspot.com

Source: https://readr.tidyverse.org/reference/read_lines.html

0 Response to "How to Read First Few Lines of a File in R"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel