如何在 R 中將 str 輸出儲存為字串?
為了在 R 中將 str 輸出儲存為字串,我們可以使用 capture.output 函式以及 str 函式。
例如,如果我們有一個名為 df 的資料框,並且我們希望將 df 的 str 輸出儲存為字串,那麼我們可以使用下面給出的命令:
capture.output(str(df))
如果我們將它儲存在一個名為 str_df<- capture.output(str(df)) 的物件中會更好,這樣可以在當前會話的將來進行呼叫。
示例 1
為了在 R 中將 str 輸出儲存為字串,請使用下面給出的程式碼:
head(mtcars,20)
如果您執行上面給出的程式碼,它將生成以下輸出:
mpg cyl disp hp drat wt qsec vs am gear carb Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4 Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4 Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1 Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1 Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2 Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1 Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4 Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2 Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2 Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4 Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4 Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3 Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3 Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3 Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4 Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4 Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4 Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1 Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2 Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
為了在 R 中將 str 輸出儲存為字串,請將以下程式碼新增到上面的程式碼中:
str_string_mtcars<-capture.output(str(mtcars)) str_string_mtcars
輸出
如果您將上面給出的所有程式碼段作為一個程式執行,它將生成以下輸出:
[1] "'data.frame':\t32 obs. of 11 variables:" [2] " $ mpg : num 21 21 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 ..." [3] " $ cyl : num 6 6 4 6 8 6 8 4 4 6 ..." [4] " $ disp: num 160 160 108 258 360 ..." [5] " $ hp : num 110 110 93 110 175 105 245 62 95 123 ..." [6] " $ drat: num 3.9 3.9 3.85 3.08 3.15 2.76 3.21 3.69 3.92 3.92 ..." [7] " $ wt : num 2.62 2.88 2.32 3.21 3.44 ..." [8] " $ qsec: num 16.5 17 18.6 19.4 17 ..." [9] " $ vs : num 0 0 1 1 0 1 0 1 1 1 ..." [10] " $ am : num 1 1 1 0 0 0 0 0 0 0 ..." [11] " $ gear: num 4 4 4 3 3 3 3 4 4 4 ..." [12] " $ carb: num 4 4 1 1 2 1 4 2 2 4 ..."
示例 2
為了在 R 中將 str 輸出儲存為字串,請使用下面給出的程式碼:
head(CO2,20)
如果您執行上面給出的程式碼,它將生成以下輸出:
Grouped Data: uptake ~ conc | Plant Plant Type Treatment conc uptake 1 Qn1 Quebec nonchilled 95 16.0 2 Qn1 Quebec nonchilled 175 30.4 3 Qn1 Quebec nonchilled 250 34.8 4 Qn1 Quebec nonchilled 350 37.2 5 Qn1 Quebec nonchilled 500 35.3 6 Qn1 Quebec nonchilled 675 39.2 7 Qn1 Quebec nonchilled 1000 39.7 8 Qn2 Quebec nonchilled 95 13.6 9 Qn2 Quebec nonchilled 175 27.3 10 Qn2 Quebec nonchilled 250 37.1 11 Qn2 Quebec nonchilled 350 41.8 12 Qn2 Quebec nonchilled 500 40.6 13 Qn2 Quebec nonchilled 675 41.4 14 Qn2 Quebec nonchilled 1000 44.3 15 Qn3 Quebec nonchilled 95 16.2 16 Qn3 Quebec nonchilled 175 32.4 17 Qn3 Quebec nonchilled 250 40.3 18 Qn3 Quebec nonchilled 350 42.1 19 Qn3 Quebec nonchilled 500 42.9 20 Qn3 Quebec nonchilled 675 43.9
為了在 R 中將 str 輸出儲存為字串,請將以下程式碼新增到上面的程式碼中:
str_string_CO2<-capture.output(str(CO2)) str_string_CO2
輸出
如果您將上面給出的所有程式碼段作為一個程式執行,它將生成以下輸出:
[1] "Classes ‘nfnGroupedData’, ‘nfGroupedData’, ‘groupedData’ and 'data.frame':\t84 obs. of 5 variables:" [2] " $ Plant : Ord.factor w/ 12 levels \"Qn1\"<\"Qn2\"<\"Qn3\"<..: 1 1 1 1 1 1 1 2 2 2 ..." [3] " $ Type : Factor w/ 2 levels \"Quebec\",\"Mississippi\": 1 1 1 1 1 1 1 1 1 1 ..." [4] " $ Treatment: Factor w/ 2 levels \"nonchilled\",\"chilled\": 1 1 1 1 1 1 1 1 1 1 ..." [5] " $ conc : num 95 175 250 350 500 675 1000 95 175 250 ..." [6] " $ uptake : num 16 30.4 34.8 37.2 35.3 39.2 39.7 13.6 27.3 37.1 ..." [7] " - attr(*, \"formula\")=Class 'formula' language uptake ~ conc | Plant" [8] " .. ..- attr(*, \".Environment\")=<environment: R_EmptyEnv> " [9] " - attr(*, \"outer\")=Class 'formula' language ~Treatment * Type" [10] " .. ..- attr(*, \".Environment\")=<environment: R_EmptyEnv> " [11] " - attr(*, \"labels\")=List of 2" [12] " ..$ x: chr \"Ambient carbon dioxide concentration\"" [13] " ..$ y: chr \"CO2 uptake rate\"" [14] " - attr(*, \"units\")=List of 2" [15] " ..$ x: chr \"(uL/L)\"" [16] " ..$ y: chr \"(umol/m^2 s)\""
示例 3
為了在 R 中將 str 輸出儲存為字串,請使用下面給出的程式碼:
head(women,20)
如果您執行上面給出的程式碼,它將生成以下輸出:
height weight 1 58 115 2 59 117 3 60 120 4 61 123 5 62 126 6 63 129 7 64 132 8 65 135 9 66 139 10 67 142 11 68 146 12 69 150 13 70 154 14 71 159 15 72 164
為了在 R 中將 str 輸出儲存為字串,請將以下程式碼新增到上面的程式碼中:
str_string_women<-capture.output(str(women)) str_string_women
輸出
如果您將上面給出的所有程式碼段作為一個程式執行,它將生成以下輸出:
[1] "'data.frame':\t15 obs. of 2 variables:" [2] " $ height: num 58 59 60 61 62 63 64 65 66 67 ..." [3] " $ weight: num 115 117 120 123 126 129 132 135 139 142 ..."
廣告