
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Save STR Output as String in R
To save the str output as a string in R, we can use capture.output function along with str function.
For example, if we have a data frame called df and we want to store the str output of df as a string then we can use the command given below −
capture.output(str(df))
It would be better if we save it in an object as str_df<- capture.output(str(df)) so that it can be recalled in the future in current session.
Example 1
To save the str output as a string in R, use the code given below −
head(mtcars,20)
If you execute the above given code, it generates the following output −
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
To save the str output as a string in R, add the following code to the above code −
str_string_mtcars<-capture.output(str(mtcars)) str_string_mtcars
Output
If you execute all the above given snippets as a single program, it generates the following output −
[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 ..."
Example 2
To save the str output as a string in R, use the code given below −
head(CO2,20)
If you execute the above given code, it generates the following output −
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
To save the str output as a string in R, add the following code to the above code −
str_string_CO2<-capture.output(str(CO2)) str_string_CO2
Output
If you execute all the above given snippets as a single program, it generates the following output −
[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)\""
Example 3
To save the str output as a string in R, use the code given below −
head(women,20)
If you execute the above given code, it generates the following output −
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
To save the str output as a string in R, add the following code to the above code −
str_string_women<-capture.output(str(women)) str_string_women
Output
If you execute all the above given snippets as a single program, it generates the following output −
[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 ..."