R语言读CSV、txt文件方式以及read.table read.csv 和readr(大数据读取包)

首先准备测试数据*(mtcars)

分别为CSV.    TXT

read.table 默认形式读取CSV(×)与TXT(效果理想)

[plain]  view plain  copy
  1. <span style="font-size:14px;">①  
  2. > test<-read.table("C:/Users/admin/Desktop/test.txt",header = F)  
  3. Error in scan(file = file, what = what, sep = sep, quote = quote, dec = dec,  :   
  4.   line 1 did not have 12 elements  
  5. > test<-read.table("C:/Users/admin/Desktop/test.txt")  
  6. > str(test)  
  7. 'data.frame':   32 obs. of  11 variables:  
  8.  $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...  
  9.  $ cyl : int  6 6 4 6 8 6 8 4 4 6 ...  
  10.  $ disp: num  160 160 108 258 360 ...  
  11.  $ hp  : int  110 110 93 110 175 105 245 62 95 123 ...  
  12.  $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...  
  13.  $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...  
  14.  $ qsec: num  16.5 17 18.6 19.4 17 ...  
  15.  $ vs  : int  0 0 1 1 0 1 0 1 1 1 ...  
  16.  $ am  : int  1 1 1 0 0 0 0 0 0 0 ...  
  17.  $ gear: int  4 4 4 3 3 3 3 4 4 4 ...  
  18.  $ carb: int  4 4 1 1 2 1 4 2 2 4 ...  
  19. > attributes(test)  
  20. $names  
  21.  [1] "mpg"  "cyl"  "disp" "hp"   "drat" "wt"   "qsec" "vs"   "am"   "gear" "carb"  
  22.   
  23. $class  
  24. [1] "data.frame"  
  25.   
  26. $row.names  
  27.  [1] "Mazda RX4"           "Mazda RX4 Wag"       "Datsun 710"          "Hornet 4 Drive"       
  28.  [5] "Hornet Sportabout"   "Valiant"             "Duster 360"          "Merc 240D"            
  29.  [9] "Merc 230"            "Merc 280"            "Merc 280C"           "Merc 450SE"           
  30. [13] "Merc 450SL"          "Merc 450SLC"         "Cadillac Fleetwood"  "Lincoln Continental"  
  31. [17] "Chrysler Imperial"   "Fiat 128"            "Honda Civic"         "Toyota Corolla"       
  32. [21] "Toyota Corona"       "Dodge Challenger"    "AMC Javelin"         "Camaro Z28"           
  33. [25] "Pontiac Firebird"    "Fiat X1-9"           "Porsche 914-2"       "Lotus Europa"         
  34. [29] "Ford Pantera L"      "Ferrari Dino"        "Maserati Bora"       "Volvo 142E"  </span>  

 
 
[plain] view plain copy
  1. <span style="background-color: rgb(255, 0, 0);">②效果不理想,没有data.frame</span>  
  2. > test<-read.table("C:/Users/admin/Desktop/test.csv")  
  3. #变量类型识别遗漏  
  4. > str(test)  
  5. 'data.frame':   33 obs. of  2 variables:  
  6.  $ V1: Factor w/ 33 levels "","AMC Javelin",..: 1 19 20 6 14 15 32 8 22 21 ...  
  7.  $ V2: Factor w/ 33 levels ",\"mpg\",\"cyl\",\"disp\",\"hp\",\"drat\",\"wt\",\"qsec\",\"vs\",\"am\",\"gear\",\"carb\"",..: 1 20 21 25 23 16 15 5 27 26 ...  
  8. > attributes(test)  
  9. $names  
  10. [1] "V1" "V2"  
  11.   
  12. $class  
  13. [1] "data.frame"  
  14.   
  15. $row.names  
  16.  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33  
修改后:还可以具体根据自己需要 ③
> test<-read.table("C:/Users/admin/Desktop/test.csv",header = T,sep=",")
> str(test)
'data.frame':	32 obs. of  12 variables:
 $ X   : Factor w/ 32 levels "AMC Javelin",..: 18 19 5 13 14 31 7 21 20 22 ...
 $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
 $ cyl : int  6 6 4 6 8 6 8 4 4 6 ...
 $ disp: num  160 160 108 258 360 ...
 $ hp  : int  110 110 93 110 175 105 245 62 95 123 ...
 $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
 $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
 $ qsec: num  16.5 17 18.6 19.4 17 ...
 $ vs  : int  0 0 1 1 0 1 0 1 1 1 ...
 $ am  : int  1 1 1 0 0 0 0 0 0 0 ...
 $ gear: int  4 4 4 3 3 3 3 4 4 4 ...
 $ carb: int  4 4 1 1 2 1 4 2 2 4 ...
> attributes(test)
$names
 [1] "X"    "mpg"  "cyl"  "disp" "hp"   "drat" "wt"   "qsec" "vs"   "am"   "gear" "carb"

$class
[1] "data.frame"

$row.names
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
④效果同③   read.table  是读取矩形格子状数据最为便利的方式
> test<-read.csv("C:/Users/admin/Desktop/test.csv",head=T,sep=",")
> str(test)
'data.frame':	32 obs. of  12 variables:
 $ X   : Factor w/ 32 levels "AMC Javelin",..: 18 19 5 13 14 31 7 21 20 22 ...
 $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...
 $ cyl : int  6 6 4 6 8 6 8 4 4 6 ...
 $ disp: num  160 160 108 258 360 ...
 $ hp  : int  110 110 93 110 175 105 245 62 95 123 ...
 $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...
 $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...
 $ qsec: num  16.5 17 18.6 19.4 17 ...
 $ vs  : int  0 0 1 1 0 1 0 1 1 1 ...
 $ am  : int  1 1 1 0 0 0 0 0 0 0 ...
 $ gear: int  4 4 4 3 3 3 3 4 4 4 ...
 $ carb: int  4 4 1 1 2 1 4 2 2 4 ...
> attributes(test)
$names
 [1] "X"    "mpg"  "cyl"  "disp" "hp"   "drat" "wt"   "qsec" "vs"   "am"   "gear" "carb"

$class
[1] "data.frame"

$row.names
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
⑤:read.csv读txt。丢失数据结构,1 variable
> test<-read.csv("C:/Users/admin/Desktop/test.txt",head=T,sep=",")
> str(test)
'data.frame':	32 obs. of  1 variable:
 $ mpg.cyl.disp.hp.drat.wt.qsec.vs.am.gear.carb: Factor w/ 32 levels "AMC Javelin 15.2 8 304 150 3.15 3.435 17.3 0 0 3 2",..: 18 19 5 13 14 31 7 21 20 22 ...
> attributes(text)
NULL
> attributes(test)
$names
[1] "mpg.cyl.disp.hp.drat.wt.qsec.vs.am.gear.carb"

$class
[1] "data.frame"

$row.names
 [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
⑥使用readr包中read_csv读取情况,其适合

[plain]  view plain  copy
  1. > test<-read_csv("C:/Users/admin/Desktop/test.csv")  
  2. Parsed with column specification:  
  3. cols(  
  4.   X1 = col_character(),  
  5.   mpg = col_double(),  
  6.   cyl = col_integer(),  
  7.   disp = col_double(),  
  8.   hp = col_integer(),  
  9.   drat = col_double(),  
  10.   wt = col_double(),  
  11.   qsec = col_double(),  
  12.   vs = col_integer(),  
  13.   am = col_integer(),  
  14.   gear = col_integer(),  
  15.   carb = col_integer()  
  16. )  
  17. Warning message:  
  18. Missing column names filled in: 'X1' [1]   
  19. > test  
  20. # A tibble: 32 × 12  
  21.                   X1   mpg   cyl  disp    hp  drat    wt  qsec    vs    am  gear  carb  
  22.                <chr> <dbl> <int> <dbl> <int> <dbl> <dbl> <dbl> <int> <int> <int> <int>  
  23. 1          Mazda RX4  21.0     6 160.0   110  3.90 2.620 16.46     0     1     4     4  
  24. 2      Mazda RX4 Wag  21.0     6 160.0   110  3.90 2.875 17.02     0     1     4     4  
  25. 3         Datsun 710  22.8     4 108.0    93  3.85 2.320 18.61     1     1     4     1  
  26. 4     Hornet 4 Drive  21.4     6 258.0   110  3.08 3.215 19.44     1     0     3     1  
  27. 5  Hornet Sportabout  18.7     8 360.0   175  3.15 3.440 17.02     0     0     3     2  
  28. 6            Valiant  18.1     6 225.0   105  2.76 3.460 20.22     1     0     3     1  
  29. 7         Duster 360  14.3     8 360.0   245  3.21 3.570 15.84     0     0     3     4  
  30. 8          Merc 240D  24.4     4 146.7    62  3.69 3.190 20.00     1     0     4     2  
  31. 9           Merc 230  22.8     4 140.8    95  3.92 3.150 22.90     1     0     4     2  
  32. 10          Merc 280  19.2     6 167.6   123  3.92 3.440 18.30     1     0     4     4  
  33. # ... with 22 more rows<pre code_snippet_id="2469924" snippet_file_name="blog_20170704_3_6619536" tabindex="0" class="GGHFMYIBMOB" id="rstudio_console_output" style="font-family: 'Lucida Console'; font-size: 10pt !important; outline: none; border: none; word-break: break-all; margin: 0px; -webkit-user-select: text; white-space: pre-wrap !important; line-height: 15px; color: rgb(0, 0, 0); font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; orphans: auto; text-align: -webkit-left; text-indent: 0px; text-transform: none; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px; background-color: rgb(255, 255, 255);" name="code"><pre code_snippet_id="2469924" snippet_file_name="blog_20170704_3_6619536" name="code" class="plain">> str(test)  
  34. Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   32 obs. of  12 variables:  
  35.  $ X1  : chr  "Mazda RX4" "Mazda RX4 Wag" "Datsun 710" "Hornet 4 Drive" ...  
  36.  $ mpg : num  21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ...  
  37.  $ cyl : int  6 6 4 6 8 6 8 4 4 6 ...  
  38.  $ disp: num  160 160 108 258 360 ...  
  39.  $ hp  : int  110 110 93 110 175 105 245 62 95 123 ...  
  40.  $ drat: num  3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ...  
  41.  $ wt  : num  2.62 2.88 2.32 3.21 3.44 ...  
  42.  $ qsec: num  16.5 17 18.6 19.4 17 ...  
  43.  $ vs  : int  0 0 1 1 0 1 0 1 1 1 ...  
  44.  $ am  : int  1 1 1 0 0 0 0 0 0 0 ...  
  45.  $ gear: int  4 4 4 3 3 3 3 4 4 4 ...  
  46.  $ carb: int  4 4 1 1 2 1 4 2 2 4 ...  
  47.  - attr(*, "spec")=List of 2  
  48.   ..$ cols   :List of 12  
  49.   .. ..$ X1  : list()  
  50.   .. .. ..- attr(*, "class")= chr  "collector_character" "collector"  
  51. c"  
  52. > attributes(test)  
  53. $class  
  54. [1] "tbl_df"     "tbl"        "data.frame"  
  55.   
  56. $row.names  
  57.  [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32  
  58.   
  59. $names  
  60.  [1] "X1"   "mpg"  "cyl"  "disp" "hp"   "drat" "wt"   "qsec" "vs"   "am"   "gear" "carb"  
  61.   
  62. $spec  
  63. cols(  
  64.   X1 = col_character(),  
  65.   mpg = col_double(),  
  66.   cyl = col_integer(),  
  67.   disp = col_double(),  
  68.   hp = col_integer(),  
  69.   drat = col_double(),  
  70.   wt = col_double(),  
  71.   qsec = col_double(),  
  72.   vs = col_integer(),  
  73.   am = col_integer(),  
  74.   gear = col_integer(),  
  75.   carb = col_integer()  
  76. )</pre><span style="font-size:18px">⑦read_csv对于test.txt ×</span><br>  
  77. <pre code_snippet_id="2469924" snippet_file_name="blog_20170704_4_3969577" name="code" class="plain">> test<-read_csv("C:/Users/admin/Desktop/test.txt")  
  78. Parsed with column specification:  
  79. cols(  
  80.   `mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear" "carb` = col_character()  
  81. )  
  82. Warning: 64 parsing failures.  
  83. row                                                              col           expected actual  
  84.   1 mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear" "carb delimiter or quote         
  85.   1 mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear" "carb delimiter or quote      M  
  86.   1 mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear" "carb delimiter or quote         
  87.   1 mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear" "carb delimiter or quote      D  
  88.   1 mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear" "carb delimiter or quote         
  89. ... ................................................................ .................. ......  
  90. See problems(...) for more details.  
  91.   
  92. > test  
  93. # A tibble: 1 × 1  
  94.                                                              `mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear" "carb`  
  95.                                                                                                                           <chr>  
  96. 1 Mazda RX4" 21 6 160 110 3.9 2.62 16.46 0 1 4 4\r\n"Mazda RX4 Wag" 21 6 160 110 3.9 2.875 17.02 0 1 4 4\r\n"Datsun 710" 22.8 4  
  97. > str(test)  
  98. Classes ‘tbl_df’, ‘tbl’ and 'data.frame':   1 obs. of  1 variable:  
  99.  $ mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear" "carb: chr "Mazda RX4\" 21 6 160 110 3.9 2.62 16.46 0 1 4 4\r\n\"Mazda RX4 Wag\" 21 6 160 110 3.9 2.875 17.02 0 1 4 4\r\n\"Datsun 710\" 22."| __truncated__  
  100.  - attr(*, "problems")=Classes ‘tbl_df’, ‘tbl’ and 'data.frame':    64 obs. of  4 variables:  
  101.   ..$ row     : int  1 1 1 1 1 1 1 1 1 1 ...  
  102.   ..$ col     : chr  "mpg\" \"cyl\" \"disp\" \"hp\" \"drat\" \"wt\" \"qsec\" \"vs\" \"am\" \"gear\" \"carb" "mpg\" \"cyl\" \"disp\" \"hp\" \"drat\" \"wt\" \"qsec\" \"vs\" \"am\" \"gear\" \"carb" "mpg\" \"cyl\" \"disp\" \"hp\" \"drat\" \"wt\" \"qsec\" \"vs\" \"am\" \"gear\" \"carb" "mpg\" \"cyl\" \"disp\" \"hp\" \"drat\" \"wt\" \"qsec\" \"vs\" \"am\" \"gear\" \"carb" ...  
  103.   ..$ expected: chr  "delimiter or quote" "delimiter or quote" "delimiter or quote" "delimiter or quote" ...  
  104.   ..$ actual  : chr  " " "M" " " "D" ...  
  105.  - attr(*, "spec")=List of 2  
  106.   ..$ cols   :List of 1  
  107.   .. ..$ mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear" "carb: list()  
  108.   .. .. ..- attr(*, "class")= chr  "collector_character" "collector"  
  109.   ..$ default: list()  
  110.   .. ..- attr(*, "class")= chr  "collector_guess" "collector"  
  111.   ..- attr(*, "class")= chr "col_spec"</pre><span style="font-family:FangSong_GB2312; font-size:18px"><span style="font-size:14px">See problems(...) for more details.<br>  
  112. <br>  
  113. > test<br>  
  114. # A tibble: 1 × 1<br>  
  115. `mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear" "carb`<br>  
  116. <chr><br>  
  117. 1 Mazda RX4" 21 6 160 110 3.9 2.62 16.46 0 1 4 4\r\n"Mazda RX4 Wag" 21 6 160 110 3.9 2.875 17.02 0 1 4 4\r\n"Datsun 710" 22.8 4<br>  
  118. > str(test)<br>  
  119. Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 1 obs. of 1 variable:<br>  
  120. $ mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear" "carb: chr "Mazda RX4\" 21 6 160 110 3.9 2.62 16.46 0 1 4 4\r\n\"Mazda RX4 Wag\" 21 6 160 110 3.9 2.875 17.02 0 1 4 4\r\n\"Datsun 710\" 22."| __truncated__<br>  
  121. - attr(*, "problems")=Classes ‘tbl_df’, ‘tbl’ and 'data.frame': 64 obs. of 4 variables:<br>  
  122. ..$ row : int 1 1 1 1 1 1 1 1 1 1 ...<br>  
  123. ..$ col : chr "mpg\" \"cyl\" \"disp\" \"hp\" \"drat\" \"wt\" \"qsec\" \"vs\" \"am\" \"gear\" \"carb" "mpg\" \"cyl\" \"disp\" \"hp\" \"drat\" \"wt\" \"qsec\" \"vs\" \"am\" \"gear\" \"carb" "mpg\" \"cyl\" \"disp\" \"hp\" \"drat\" \"wt\" \"qsec\" \"vs\" \"am\"  
  124.  \"gear\" \"carb" "mpg\" \"cyl\" \"disp\" \"hp\" \"drat\" \"wt\" \"qsec\" \"vs\" \"am\" \"gear\" \"carb" ...<br>  
  125. ..$ expected: chr "delimiter or quote" "delimiter or quote" "delimiter or quote" "delimiter or quote" ...<br>  
  126. ..$ actual : chr " " "M" " " "D" ...<br>  
  127. - attr(*, "spec")=List of 2<br>  
  128. ..$ cols :List of 1<br>  
  129. .. ..$ mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear" "carb: list()<br>  
  130. .. .. ..- attr(*, "class")= chr "collector_character" "collector"<br>  
  131. ..$ default: list()<br>  
  132. .. ..- attr(*, "class")= chr "collector_guess" "collector"<br>  
  133. ..- attr(*, "class")= chr "col_spec<br>  
  134. "相关更多参数<br>  
  135. <br>  
  136. read.table(file, header = FALSE, sep = "", quote = "\"'",<br>  
  137.            dec = ".", numerals = c("allow.loss", "warn.loss", "no.loss"),<br>  
  138.            row.names, col.names, as.is = !stringsAsFactors,<br>  
  139.            na.strings = "NA", colClasses = NA, nrows = -1,<br>  
  140.            skip = 0, check.names = TRUE, fill = !blank.lines.skip,<br>  
  141.            strip.white = FALSE, blank.lines.skip = TRUE,<br>  
  142.            comment.char = "#",<br>  
  143.            allowEscapes = FALSE, flush = FALSE,<br>  
  144.            stringsAsFactors = default.stringsAsFactors(),<br>  
  145.            fileEncoding = "", encoding = "unknown", text, skipNul = FALSE)<br>  
  146. <br>  
  147. 常用参数解读:<br>  
  148. file表示要读取的文件。file可以是<br>  
  149. ①绝对路径或者相对路径,但是一定要注意,因为在R语言中\是转义符,所以路径分隔符必须写成\\,比如“C:\\myfile\\myfile.txt”或者<br>  
  150. Sys.setenv(JAVA_HOME='C://Program Files/Java/jdk1.6.0_21/jre')<br>  
  151. ②可以使剪切板的内容。<br>  
  152. ③使用file.choose(),弹出对话框,让你选择文件位置。<br>  
  153. header来确定数据文件中第一行是不是标题。默认F,即认为数据文件没有标题<br>  
  154. 参数----------Arguments----------<br>  
  155. 参数:sep<br>  
  156. the field separator character.  Values on each line of the file are separated by this character.  If sep = "" (the default for read.table) the separator is "white space", that is one or more spaces, tabs, newlines or carriage returns.<br>  
  157. 字段分隔符。文件的每一行的值是通过这个角色分离。如果sep = ""(默认read.table)分隔符是“白色空间”,这是一个或多个空格,制表符,换行符或回车。<br>  
  158. 参数:quote<br>  
  159. the set of quoting characters. To disable quoting altogether, use quote = "".  See scan for the behaviour on quotes embedded in quotes.  Quoting is only considered for columns read as character, which is all of them unless colClasses is specified.<br>  
  160. 引用字符集。完全禁用引用,使用quote = ""。看到scan引号中嵌入引号的行为。只考虑读的性格,这是所有这些,除非colClasses指定的列引用。<br>  
  161. 参数:dec<br>  
  162. the character used in the file for decimal points. <br>  
  163. 字符用于在小数点文件。<br>  
  164. 参数:row.names<br>  
  165. a vector of row names.  This can be a vector giving the actual row names, or a single number giving the column of the table which contains the row names, or character string giving the name of the table column containing the row names.  If there is a header  
  166.  and the first row contains one fewer field than the number of columns, the first column in the input is used for the row names.  Otherwise if row.names is missing, the rows are numbered.  Using row.names = NULL forces row numbering. Missing or NULL row.names  
  167.  generate row names that are considered to be "automatic" (and not preserved by as.matrix).  <br>  
  168. 向量的行名。这可以是一个向量,给予实际的行名,或一个号码表,其中包含的行名,或字符串,包含行名称表列的名称列。如果有一个头的第一行包含列数少一个领域,在输入的第一列用于行名称。否则,如果row.names丢失,行编号。使用row.names = NULL部队排编号。失踪或NULLrow.names,生成的行被认为是“自动”(而不是由as.matrix保存)的名称。<br>  
  169. 参数:col.names<br>  
  170. a vector of optional names for the variables. The default is to use "V" followed by the column number.<br>  
  171. 可选名称为变量的向量。默认是使用列数"V"其次。<br>  
  172. 参数:as.is<br>  
  173. the default behavior of read.table is to convert character variables (which are not converted to logical, numeric or complex) to factors.  The variable as.is controls the conversion of columns not otherwise specified by colClasses. Its value is either a vector  
  174.  of logicals (values are recycled if necessary), or a vector of numeric or character indices which specify which columns should not be converted to factors.  Note: to suppress all conversions including those of numeric columns, set colClasses = "character".   
  175.  Note that as.is is specified per column (not per variable) and so includes the column of row names (if any) and any columns to be skipped.  <br>  
  176. read.table的默认行为转换成字符变量(而不是转换为逻辑,数字或复杂的)因素。变量as.is控制转换colClasses没有其他指定的列。它的值是一个逻辑值向量(如果有必要回收价值),或数字或字符索引指定的列不应该被转换为因素的向量。注:禁止所有的转换,包括那些数字列,设置colClasses = "character"。请注意,as.is指定每列(而不是每个变量)等行名称的列(如有)及任何要跳过的列。<br>  
  177. 参数:na.strings<br>  
  178. a character vector of strings which are to be interpreted as NA values.  Blank fields are also considered to be missing values in logical, integer, numeric and complex fields.<br>  
  179. NA值作为解释的字符串的字符向量。空白领域也被认为是缺少逻辑,整数,数字和复杂的领域中的价值。<br>  
  180. 参数:colClasses<br>  
  181. character.  A vector of classes to be assumed for the columns.  Recycled as necessary, or if the character vector is named, unspecified values are taken to be NA.  Possible values are NA (the default, when type.convert is used), "NULL" (when the column is skipped),  
  182.  one of the atomic vector classes (logical, integer, numeric, complex, character, raw), or "factor", "Date" or "POSIXct".  Otherwise there needs to be an as method (from package methods) for conversion from "character" to the specified formal class.  Note that  
  183.  colClasses is specified per column (not per variable) and so includes the column of row names (if any).  <br>  
  184. 字符。须承担一个班的向量为列。必要时,回收或如果被命名为特征向量,未指定的值是NA。可能的值是NA(默认情况下,当type.convert)"NULL"(列时跳过),一个原子的向量类(逻辑,整数,数字,复杂的,性格,原材料),或"factor","Date"或"POSIXct"。否则需要有一个as从methods转换到指定的正规类的方法(包"character")。请注意,colClasses指定每列(而不是每个变量)等行名称(如有)列。<br>  
  185. 参数:nrows<br>  
  186. integer: the maximum number of rows to read in.  Negative and other invalid values are ignored.<br>  
  187. 整数:最大数量的行读入负和其他无效值将被忽略。<br>  
  188. 参数:skip<br>  
  189. integer: the number of lines of the data file to skip before beginning to read data.<br>  
  190. 整数:开始读取数据前跳过的数据文件的行数。<br>  
  191. 参数:check.names<br>  
  192. logical.  If TRUE then the names of the variables in the data frame are checked to ensure that they are syntactically valid variable names.  If necessary they are adjusted (by make.names) so that they are, and also to ensure that there are no duplicates.<br>  
  193. 逻辑。如果TRUE然后检查数据框中的变量的名称,以确保它们是语法上有效的变量名。如果有必要,他们调整(make.names),使他们,同时也确保没有重复。<br>  
  194. 参数:fill<br>  
  195. logical. If TRUE then in case the rows have unequal length, blank fields are implicitly added.  See "Details".<br>  
  196. 逻辑。如果TRUE然后在情况下,行有长度不等的空白领域隐式添加。见“详细资料”。<br>  
  197. 参数:strip.white<br>  
  198. logical. Used only when sep has been specified, and allows the stripping of leading and trailing white space from unquoted character fields (numeric fields are always stripped).  See scan for further details (including the exact meaning of "white space"), remembering  
  199.  that the columns may include the row names. <br>  
  200. 逻辑。只用当sep已指定,并允许剥离的非上市character(numeric领域总是剥离领域)的开头和结尾的空白。看到scan进一步详情(包括“白色空间”的确切含义),记住,列可能包含的行名。<br>  
  201. 参数:blank.lines.skip<br>  
  202. logical: if TRUE blank lines in the input are ignored. <br>  
  203. 逻辑:如果TRUE在输入空行被忽略。<br>  
  204. 参数:comment.char<br>  
  205. character: a character vector of length one containing a single character or an empty string.  Use "" to turn off the interpretation of comments altogether.<br>  
  206. 性格:特征向量的长度包含单个字符或一个空字符串之一。使用""完全关闭评论的解释。<br>  
  207. 参数:allowEscapes<br>  
  208. logical.  Should C-style escapes such as \n be processed or read verbatim (the default)?   Note that if not within quotes these could be interpreted as a delimiter (but not as a comment character).  For more details see scan.<br>  
  209. 逻辑。如\n处理或逐字读(默认)C风格逃逸?请注意,如果不是引号内的这些都可以解释为分隔符(而不是作为一个注释字符)。详细内容见scan。<br>  
  210. 参数:flush<br>  
  211. logical: if TRUE, scan will flush to the end of the line after reading the last of the fields requested. This allows putting comments after the last field.<br>  
  212. 逻辑:如果TRUE,scan将刷新行结束后阅读领域的最后要求。这允许把意见后,最后一个字段。<br>  
  213. 参数:stringsAsFactors<br>  
  214. logical: should character vectors be converted to factors?  Note that this is overridden by as.is and colClasses, both of which allow finer control.<br>  
  215. 逻辑:特征向量转换的因素?请注意,这是由as.is和colClasses,这两者可以更好地控制覆盖。<br>  
  216. 参数:fileEncoding<br>  
  217. character string: if non-empty declares the encoding used on a file (not a connection) so the character data can be re-encoded.  See the "Encoding" section of the help for file, the "R Data Import/Export Manual" and "Note".  <br>  
  218. 字符串:如果非空的声明文件(未连接)上使用这样的字符数据可以被重新编码的编码。看到“编码”部分,帮助file“R数据导入/导出手册”和“注意”。<br>  
  219. 参数:encoding<br>  
  220. encoding to be assumed for input strings.  It is used to mark character strings as known to be in Latin-1 or UTF-8 (see Encoding): it is not used to re-encode the input, but allows R to handle encoded strings in their native encoding (if one of those two).   
  221.  See "Value".  <br>  
  222. 假设输入字符串编码。它是用来作为已知的Latin-1或UTF-8(见标记字符串Encoding):不使用它来重新编码输入,但允许R在他们的本地编码处理编码的字符串(如果这两个标准之一)。看到“价值”。<br>  
  223. 参数:text<br>  
  224. character string: if file is not supplied and this is,  then data are read from the value of text via a text connection. Notice that a literal string can be used to include (small) data sets  within R code.   <br>  
  225. 字符串:file如果不提供的,这是,那么数据是从text值读通过的文本连接。请注意,一个文字字符串,可用于包括(小)R代码集内的数据。<br>  
  226. 参数:...<br>  
  227. Further arguments to be passed to read.table. <br>  
  228. 进一步的参数被传递到read.table。<br>  
  229. <br>  
  230. 和read.table有所不同的,是read.csv的默认参数有别。注意看,header和sep的默认值。<br>  
  231. <br>  
  232. read.csv(file, header = TRUE, sep = ",", quote = "\"",<br>  
  233.          dec = ".", fill = TRUE, comment.char = "", ...)<br>  
  234. </span><br>  
  235. <br>  
  236. </span>  
  237. <p></p>  
  238. <div title="Google Translator Anywhere" class="itanywhere-activator" style="left:350px; top:6062px">  
  239. </div>  
  240. <pre></pre>  
  241. <pre></pre>  
  242. <div title="Google Translator Anywhere" class="itanywhere-activator" style="left:1px; top:4658px">  
  243. </div>  
  244. <pre></pre>  
  245. <pre></pre>  
  246. <div title="Google Translator Anywhere" class="itanywhere-activator" style="left:440px; top:4276px">  
  247. </div>  
  248.      
  249. </pre>  
  • 4
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
r语言中的read.csv函数用于读取以逗号分隔的文件(csv文件)。在使用该函数时,可以通过设置stringsAsFactors参数为FALSE,来确保R不会将字符或类别变量转换为因子。下面是一个示例代码: ```R data1 <- read.csv("C:\\Users\\Bob\\Desktop\\data.csv", header=TRUE, stringsAsFactors=FALSE) ``` 上述代码中,读取了名为"data.csv"的文件,并将数据存储在名为data1的数据框中。通过设置stringsAsFactors参数为FALSE,确保字符或类别变量不会被转换为因子。使用str函数可以查看数据框的结构,如下所示: ```R str(data1) ``` 上述代码会显示数据框data1的结构,括变量名、变量类型和前几行的值。其中,变量team是字符型,points和assists是整型。 此外,还可以使用其他方法读取csv文件,比如使用readrread_csv方法或R原生的read_csv方法。这些方法可能具有不同的性能特点。例如,read_csv方法是readr中的函数,它被认为比read.csv函数更快。下面是使用read_csv方法读取csv数据的示例代码: ```R library(readr) data2 <- read_csv("C:\\Users\\Bob\\Desktop\\data.csv") ``` 上述代码中,使用read_csv方法读取了名为"data.csv"的文件,并将数据存储在名为data2的数据框中。再次使用str函数可以查看data2的结构。 另外,还可以使用data.table读取csv数据。该被认为比read.csv函数更快。下面是使用data.table读取csv数据的示例代码: ```R library(data.table) data3 <- fread("C:\\Users\\Bob\\Desktop\\data.csv") ``` 上述代码中,使用fread函数从名为"data.csv"的文件读取数据,并将数据存储在名为data3的数据表中。同样,可以使用str函数查看data3的结构。 综上所述,r语言中有多种方法可以读取csv数据read.csv函数、readrread_csv方法和data.table的fread函数。可以根据具体需求选择适合的方法。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值