R语言学习基本指令读取文件以及数据

R语言基本指令,本文主要focus在读取文件以及数据介绍上

觉得自己以后可能生活中很经常会和R语言打交道,并且觉得R在处理数据上很好用并且相对于其他机器语言好学一些,决定好好学习R语言。这篇文章的内容主要也是参考其他人的博客以及coursera上JHU和duke的课程,希望后期能够做到一些简单的实现。学习用的材料用的是中英双语的,所以后面的代码书写会出现两种语言。
有用的数据网站:https://www.kaggle.com/harlfoxem/housesalesprediction ## housing transactions in King County
工具:R Studio

写在前面的话

主要借鉴的我的计量老师 Rachael Meager的,也是一些编程中的好习惯吧,提醒自己
(1) ALWAYS name your variables something descriptive
(2) indent your code (use tab)
(3) make everything into a function if it can possibly be a function (always make a module)
(4) almost never hardcode a number or copy and paste pieces of code
(5) check that those canned functions do what you think they do
– especially when they encounter NAs
6) write unit tests and debug regularly and systematically
– asymmetric debugging is a form of fishing

基本指令

前期工作,文件读取等

1
2
3
4
rm(list=ls()) #clear workspace first
getwd( ) # 找到working directory
setwd("The path of the data") #import data
read.csv(“文件路径”) #读取文件 文件路径可用查看属性方式找到

R with statistics

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
***赋值***
<-,like x<-1,令x=1,此时如果 print(x),output [1] [1]表明x是一个vector and 5 is the first element
x <- 1:20 #create a sequence of 1 2 3 4 5 ...20(vector,integer)
*** creating vectors***
方式一 c function
x <- c(0.5,0.6) ##numeric
x <- c(T,F) ##logical
x <- c("a","b","c") ##character
x <- c(0+1i,2+2i) ##complex
## list is a special vector that can contain different classes
方式二 vector
x <- vector("numeric", length=10)
***converge data from one class to another***
as.logical(x),把x convert to logical class, T or F
*** creating matrices***
方法一 m <- matrix(1:6,nrow = 2, ncol =3) ##构造一个两行三列的matrix
方法二, convert from vectors by adding dim()
m <- 1:10
dim(m) <- c(2,5)
***判断数据属于哪个class,比如interger
class()