HW2_Tang

Reading CSV Data in R

Hongbing Tang
2022-06-13

Data Import & Data Types

[1] "D:/Documents/LANDD/Z_UMass PhD/_2020 Summer DACSS 601 R/Class Sean/week3"
setwd("D:/Documents/LANDD/Z_UMass PhD/_2020 Summer DACSS 601 R/Class Sean/week3")
Marinecorps_data <- read.csv("marinecorps_cleaned - marinecorps_cleaned.csv")

head(Marinecorps_data) 
  enlisted pay_grade single.withoutchildren.male
1        E         1                        6232
2        E         2                       15916
3        E         3                       34868
4        E         4                       17862
5        E         5                        7490
6        E         6                        2051
  single.withoutchildren.female single.withoutchildren.total
1                           583                         6815
2                          1336                        17252
3                          1864                        36732
4                          1026                        18888
5                           590                         8080
6                           208                         2259
  single.withchildren.male single.withchildren.female
1                       54                          3
2                      190                         12
3                      574                        167
4                      527                        272
5                      931                        338
6                     1060                        226
  single.withchildren.total married.jointservice.male
1                        57                        20
2                       202                        98
3                       741                       780
4                       799                      1070
5                      1269                      1199
6                      1286                       568
  married.jointservice.female married.jointservice.total
1                          19                         39
2                         109                        207
3                         928                       1708
4                         944                       2014
5                         730                       1929
6                         341                        909
  married.civilian.female married.civilian.male
1                     611                    21
2                    2399                    96
3                   14643                   344
4                   15599                   414
5                   17921                   344
6                   12005                   221
  married.civilian.total married.male.total married.female.total
1                    632               6917                  626
2                   2495              18603                 1553
3                  14987              50865                 3303
4                  16013              35058                 2656
5                  18265              27541                 2002
6                  12226              15684                  996
  married.total.total      branch
1                7543 MarineCorps
2               20156 MarineCorps
3               54168 MarineCorps
4               37714 MarineCorps
5               29543 MarineCorps
6               16680 MarineCorps
typeof(Marinecorps_data$enlisted)
[1] "character"
typeof(Marinecorps_data$pay_grade)
[1] "integer"
typeof(Marinecorps_data$branch)
[1] "character"
Marinecorps_data <- read_csv("../week3/marinecorps_cleaned - marinecorps_cleaned.csv")
spec(Marinecorps_data)
cols(
  enlisted = col_character(),
  pay_grade = col_double(),
  `single withoutchildren male` = col_double(),
  `single withoutchildren female` = col_double(),
  `single withoutchildren total` = col_double(),
  `single withchildren male` = col_double(),
  `single withchildren female` = col_double(),
  `single withchildren total` = col_double(),
  `married jointservice male` = col_double(),
  `married jointservice female` = col_double(),
  `married jointservice total` = col_double(),
  `married civilian female` = col_double(),
  `married civilian male` = col_double(),
  `married civilian total` = col_double(),
  `married male total` = col_double(),
  `married female total` = col_double(),
  `married total total` = col_double(),
  branch = col_character()
)
# Note 
# spec(Marinecorps_data) gets an error message if running alone as it can't find the file. After checking 6/5 "Data Import" blog again, I add a line to show the path.
# it works by reading again! Add the following before runs spec()
# Marinecorps_data <- read_csv("../week3/marinecorps_cleaned - marinecorps_cleaned.csv")   

There are both numerical and string data in this dataset. For example, The data in the columns “enlisted” and “branch” are categorized as “character”, which are string data, and the data in the column “married female total” are categorized as “double”, which are double-precision numerical data.

Additional practices

library(dplyr)
Marinecorps_data_1<-select(Marinecorps_data, 1, 2, 5)
Marinecorps_data_1
# A tibble: 24 × 3
   enlisted pay_grade `single withoutchildren total`
   <chr>        <dbl>                          <dbl>
 1 E                1                           6815
 2 E                2                          17252
 3 E                3                          36732
 4 E                4                          18888
 5 E                5                           8080
 6 E                6                           2259
 7 E                7                            637
 8 E                8                            169
 9 E                9                             78
10 O                1                           2431
# … with 14 more rows
knitr::kable(Marinecorps_data_1)
enlisted pay_grade single withoutchildren total
E 1 6815
E 2 17252
E 3 36732
E 4 18888
E 5 8080
E 6 2259
E 7 637
E 8 169
E 9 78
O 1 2431
O 2 1642
O 3 1710
O 4 381
O 5 83
O 6 30
O 7 3
O 8 0
O 9 0
O 10 1
W 1 29
W 2 33
W 3 22
W 4 6
W 5 6