8.2: Describing...
Firstly, look on the basic characteristics of every character:
Code \(\PageIndex{1}\) (R):
Since SEX and COLOR are categorical, the output in these columns has no sense, but you may want to convert these columns into “true” categorical data. There are multiple possibilities but the simplest is the conversion into factor :
Code \(\PageIndex{2}\) (R):
(To retain the original data, we copied it first into new object data1 . Please check it now with summary() yourself.)
summary() command is applicable not only to the whole data frame but also to individual characters (or variables, or columns):
Code \(\PageIndex{3}\) (R):
It is possible to calculate characteristics from summary() one by one. Maximum and minimum:
Code \(\PageIndex{4}\) (R):
... median:
Code \(\PageIndex{5}\) (R):
... mean for WEIGHT and for each character:
Code \(\PageIndex{6}\) (R):
and
Code \(\PageIndex{7}\) (R):
... and also round the result to one decimal place:
Code \(\PageIndex{8}\) (R):
(Again, the output of colMeans() has no sense for SEX and COLOR .)
Unfortunately, the commands above (but not summary() ) do not work if the data have missed values ( NA ):
Code \(\PageIndex{9}\) (R):
To calculate mean without noticing missing data, enter
Code \(\PageIndex{10}\) (R):
Another way is to remove rows with NA from the data with:
Code \(\PageIndex{11}\) (R):
Then, data2.o will be free from missing values.
Sometimes, you need to calculate the sum of all character values:
Code \(\PageIndex{12}\) (R):
... or the sum of all values in one row (we will try the second row):
Code \(\PageIndex{13}\) (R):
... or the sum of all values for every row:
Code \(\PageIndex{14}\) (R):
(These summarizing exercises are here for training purposes only.)
For the categorical data, it is sensible to look how many times every value appear in the data file (and that also help to know all values of the character):
Code \(\PageIndex{15}\) (R):
Now transform frequencies into percents (100% is the total number of bugs):
Code \(\PageIndex{16}\) (R):
One of the most important characteristics of data variability is the standard deviation :
Code \(\PageIndex{17}\) (R):
Calculate standard deviation for each numerical column (columns 3 and 4):
Code \(\PageIndex{18}\) (R):
If you want to do the same for data with a missed value, you need something like:
Code \(\PageIndex{19}\) (R):
Calculate also the coefficient of variation (CV):
Code \(\PageIndex{20}\) (R):
We can calculate any characteristic separately for males and females. Means for insect weights:
Code \(\PageIndex{21}\) (R):
How many individuals of each color are among males and females?
Code \(\PageIndex{22}\) (R):
(Rows are colors, columns are males and females.)
Now the same in percents:
Code \(\PageIndex{23}\) (R):
Finally, calculate mean values of weight separately for every combination of color and sex (i.e., for red males, red females, green males, green females, and so on):
Code \(\PageIndex{24}\) (R):