Datacamp: Introduction to Importing Data in R

Chapter 1

1. Introduction & read.csv

    Defaults: (1) header = TRUE 

                   (2) sep = ","

    eg. (1) Use read.csv() to import "swimming_pools.csv" as a data frame with the name pools.(2) Print the structure of pools using str().

# Import swimming_pools.csv: pools
pools <- read.csv("swimming_pools.csv")

# Print the structure of pools
str(pools)

    stringsAsFactors: If seting stringsAsFactors to FALSE, the data frame columns corresponding to strings in your text file will be character. If seting stringsAsFactors to TRUE, you import strings as factors.

# Import swimming_pools.csv correctly: pools
pools <- read.csv("swimming_pools.csv", stringsAsFactors = FALSE)

# Check the structure of pools
str(pools)

2. read.delim & read.table

    (1) read.delim (读取.txt文件)

         Default: (1) header = TRUE (the first row contains the field names).

                      (2) sep = "\t" (fields in a record are delimited by tabs)

    eg1. (1) Import the data in "hotdogs.txt" with read.delim(). Call the resulting data frame hotdogs and the variable names are not on the first line. (2) Call summary() on hotdogs. This will print out some summary statistics about all variables in the data frame.

# Import hotdogs.txt: hotdogs
hotdogs <- read.delim("hotdogs.txt", header = FALSE)

# Summarize hotdogs
summary(hotdogs)

    (2) read.table (deal with more exotic flat file formats)

         By default, the header argument defaults to FALSE and the sep argument is "" by default.

    eg2. (1) The data is still hotdogs.txt. It has no column names in the first row, and the field separators are tabs. This time, though, the file is in the data folder inside your current working directory. A variable path with the location of this file is already coded for you. (2) Call head() on hotdogs; this will print the first 6 observations in the data frame.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值