Read.table () in the R language

Source: Internet
Author: User

参考资料:http://www.cnblogs.com/xianghang123/archive/2012/06/06/2538274.html

Read.table (file, Header = FALSE, Sep = "", quote = "\" ",           Dec =". ", numerals = C (" Allow.loss "," Warn.loss "," No.loss " ),           row.names, col.names, as.is =!stringsasfactors,           na.strings = "NA", colclasses = na, nrows =-1,           skip = 0, Check.names = true, fill =!blank.lines.skip,           strip.white = FALSE, Blank.lines.skip = True,           Comment.char = "#", 
   allowescapes = false, flush = False,           stringsasfactors = Default.stringsasfactors (),           fileencoding = "", encoding = "Unknown", text, Skipnul = FALSE)

  • FILE specifies read-in files

  • Header has column name (default none)

  • SEQ Specifies the delimiter (space, TAB, line feed, carriage return)

  • Quote a character that encloses character data. By default, a string can be "or" enclosed, and in both cases, the inner character of the quotation mark is nonalphanumeric as part of the string. The set of valid reference characters (possibly not) is controlled by the parameter quote. Default value changed to quote = ""

  • Dec = "." Specifies the number of decimal points

  • Colclasses the data type format of the specified column

  • Row.names specify each row name, or a number, to specify a column row name

  • Col.names

  • as.is =!stringsasfactors as.is character vectors convert Genesis (only this function),true when reserved as character type

  • na.strings = "NA" Specifies what character represents a missing value

  • colclasses = NA colclasses Run sets the required type for each column in the input. Note that colclasses and as.is are dedicated to each column, not to each variable. Therefore, it also applies to row label columns (if any).

  • nrows = 1 The maximum number of reads, that is, how many lines are read in, and "1" means that both reads

  • Skip = 0 Skips the first n rows of a file (skip = N)

  • Check.names = TRUE # Check that the variable name is valid in R

  • Fill =!blank.lines.skip files that are exported from a spreadsheet will typically have trailing empty fields (including the Sungaia? Ignored. In order to read such a file, the parameter must be set to fill = TRUE

  • Strip.white = FALSE If a delimiter is set, the whitespace at the beginning and end of the character field is treated as part of the field. To remove these blanks, you can use the parameter Strip.white = TRUE

  • Blank.lines.skip = TRUE By default, Read.table ignores blank lines. This can be changed by setting Blank.lines.skip = FALSE. However, this parameter is valid only if it is used in conjunction with fill = TRUE. At this point, a blank line may be used to indicate a defective sample in the rule data.

  • Comment.char = "#" By default, Read.table uses # as the comment identifier character. If the character is encountered (except within the referenced string), the subsequent contents of the row are ignored. Lines that contain only whitespace and comments are treated as blank lines. If there is no comment in the data file, using Comment.char = "" will be more secure (and may make it faster).

  • Allowescapes = falseread.table and Scan have a logical parameter allowescapes. Starting with R 2.2.0, this parameter defaults to no, and the backslash is the only character that is interpreted as an escape reference (in the environment described earlier). If this parameter is set to Yes, the escape rule in the form of C is interpreted, that is, control such as,,,,, octal and hexadecimal as described in the same as 40 and x2a. Any other escape characters are looking at themselves, including the backslash

Women1.txt

nameheightweighttmpstu1581151.1stu2591171.2stu3601201.3stu4611231.4stu5621261.5stu6631291.6stu7641321.7stu8651351.8stu966 1391.9stu10671422stu11681462.1stu12691502.2stu13701542.3stu14711592.4stu15721642.5

# # Basic Parameters DataSet1 <-read.table ("./women1.txt", Header = T, Sep = "\ T") head (DataSet1) # # Name Height weight tmp## 1 stu1  1.1## 2 STU2 117 1.2## 3 STU3 4 1.3## stu4 123 1.4## 5 STU5 62 126 1.5## 6 Stu6 129 1.6dataset1$name## [1] stu1 stu2 stu3 stu4 stu5 stu6 stu7 stu8 stu9 stu10 stu11## [1 2] stu12 stu13 stu14 stu15## levels:stu1 stu10 stu11 stu12 stu13 stu14 stu15 stu2 stu3 ... stu9class (dataset1$name) # # [1] "factor" Is.factor (Dataset1$name) # # [1] TRUEdataset1 <-read.table ("./women1.txt", Header = T, Sep = "\ t", as.is = T Head (DataSet1) # # Name Height weight tmp## 1 stu1 2 1.1## 3 STU2 60 120 1. 3## 4 Stu4 123 1.4## 5 Stu5 1 126 1.5## 6 Stu6 129 1.6dataset1$name## [] "stu1" "Stu2" "St U3 "" Stu4 "" Stu5 "" Stu6 "" Stu7 "" Stu8 "# # [9]" STU9 "" Stu10 "" Stu11 "" Stu12 "" Stu13 "" Stu14 "" Stu15 "Class (dataset1$ Name) # # [1] "character" IS.factor (Dataset1$name) # [1] false## skip = 0 skips the first n rows of the file (skip = N) dataset2 <-read.table ("./women1.txt", Header = T, Sep = "\ t", skip = 3) head (Dataset2) # STU3 X60 X120 x1.3## 1 stu4 123 1.4## 2 stu5 63 126 1.5## 3 Stu6 129 1 .6## 4 Stu7 1.7## 5 stu8 135 1.8## 6 stu9 139 1.9dataset2 <-read.table ("./women1.txt", Header =  F, Sep = "\ t", skip = 3) head (Dataset2) # # V1 V2 V3 v4## 1 stu3 2 1.3## 3 Stu4 123 62 1.4## 126 Stu6 129 1.6## 5 Stu7 1 1.7## 6 stu8 1 135 1.8## nrows = The maximum number of read-in, and the "" means all read into the Dataset3 <-read.table ("./women1.     TXT ", Header = T, Sep =" \ t ", nrows = 3) head (DATASET3) # # Name Height weight tmp## 1 stu1 1.1## 2 STU2 117 1.2## 3 Stu3 1.3dataset3 <-read.table ("./women1.txt", Header = F, Sep = "\ t", nrows = 3) head (d ATASET3) # # V1 V2 V3 v4## 1 Name Height weight tmp## 2 stu1 1.1## 3 STU2 59 117 1.2## Specify line Name Dataset4 <-Read.tabLe ("./women1.txt", Header = T, Sep = "\ t", Row.names = 1) # * * The first row in the table a column element is skipped **head (DATASET4) # # Height Weight tmp## stu1 1.1## STU2 117 1.2## stu3, 1.3## stu4, 123 1.4## Stu5, 126 1.5## s TU6 129 1.6row.names (DATASET4) # # [1] "STU1" "Stu2" "Stu3" "Stu4" "Stu5" "Stu6" "Stu7" "Stu8" # # [9] "St U9 "Stu10" "Stu11" "Stu12" "Stu13" "Stu14" "STU15"

Women2.txt

\ This is some simple test data nameheightweighttmp/stu1/581151*1/stu2/591171*2/stu3/60 "" 1*3\ Note: The space test must be caused, \ Otherwise will be the first error "... line 3 did Not has 4 elements "/stu4/611231*4/stu5/62no1*5/stu6/nono1*6/stu7/641321*7/stu8/651351*8/stu9/661391*9/stu10/ Nana2*0/stu11/681462*1/stu12/691502*2/stu13/701542*3/stu14/711592*4/stu15/721642*5

# Dec = "." Specifies the number of decimal points; na.strings = "NA" Specifies what character represents a missing value; Comment.char can only set a data1 <-read.table ("./women2.txt", Header = T,    Dec = "*", na.strings = C ("", "NA", "NO"), Comment.char = "\ \") Head (data1) # # Name Height weight tmp## 1/stu1/58  1.1## 2/stu2/59 117 1.2## 3/stu3/60 na 1.3## 4/stu4/61 123 1.4## 5/stu5/62 na 1.5## 6/stu6/na NA 1.6sapply (Data1[1:6,], is.na) # # Name Height weight tmp## [1,] false false FA    lse## [2,] false false false## [3,] false TRUE false## [4,] false false false## [5,] false false True false## [6,] FALSE true Falsesapply (Data1, Class) # # Name Height weight tmp # # "factor" "Integer" "Integer" "Numeric" # Quote Settings data1 <-read.table ("./women2.txt", Header = T, Dec = "*", na.strings = C ("", "N  A "NO"), Comment.char = "\ \", quote = "/", as.is = F) Head (data1) # # Name Height weight tmp## 1 stu1 58 115 1.1## 2 STU2 59 117 1.2## 3 Stu3 "" 1.3## 4 STU4-123 1.4## 5 stu5 <NA> 1.5## 6 stu6 NA <NA> 1.6s Apply (Data1, Class) # # Name Height weight tmp # # "factor" "integer" "Factor" "numeric"

Read.table () in the R language

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.