Data Import

Author

Shifa Maqsood

#Built-in data

#data(cars)

  #this shows your data

#view(cars) 

  #one of the way you can view your data

#Importing data

# rio::import() is helpful
avatar <- readr::read_csv("data/avatar.csv")
Rows: 3 Columns: 3
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (2): name, bends
lgl (1): friendly

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
#spec() function enables us to change our data type simultaneously, first load package readr::read_whatever file type

#Creating data

 random_data <- data.frame(
   random_number = c(1,5),
   random_words = c("water", "earth")
 
     )
random_data %>%
  mutate( c("tf","hh"))
  random_number random_words c("tf", "hh")
1             1        water            tf
2             5        earth            hh

#Writing data

#try making data frame, export it, then remove it from environment
family <- data.frame(
  first_name= c("lisa", "James"),
  last_name = c("Robert", "Jones"),
  Ages = c(34,40)
)
export(family, "data/family.csv")
#import data after deleting it  from environment
try_importing <- import("data/family.csv")

Convert column types of a data set

starwars <-read_csv("data/starwars.csv")
Rows: 87 Columns: 14
── Column specification ────────────────────────────────────────────────────────
Delimiter: ","
chr (11): name, hair_color, skin_color, eye_color, sex, gender, homeworld, s...
dbl  (3): height, mass, birth_year

ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
#use this function: spec(starwars) 
 
ct <- cols(
  name = col_character(),
  height = col_factor(),
  mass = col_double(),
  hair_color = col_factor(),
  skin_color = col_factor(),
  eye_color = col_factor(),
  birth_year = col_double(),
  sex = col_factor(),
  gender = col_factor(),
  homeworld = col_factor(),
  species = col_factor(),
  films = col_skip(),
  vehicles = col_skip(),
  starships = col_skip()
)

starwars <- read_csv("data/starwars.csv",
                   col_types = ct)



starwars <-read_csv("data/starwars.csv", col_types = ct)