R 基本使用

# 查看当前工作目录
getwd()

# 设置当前工作目录
setwd('/Users/jupyter/lesson2')

# 查看当前路径下的文件
list.files()

# 读取csv数据
statesInfo <- read.csv('statesData.csv')
# 读取tsv数据
pf <- read.csv('pseudo_facebook.tsv', sep = '\t')
pf <- read.delim('pseudo_facebook.tsv')

# 获取子数据集
stateSubset <- subset(statesInfo, state.region == 1)
head(stateSubset, 2)
dim(stateSubset)

# 另一种方式 获取子数据集
statesInfo[statesInfo$state.region == 1,]
# 查看帮助
?cars

# 查看r自带的数据集cars 的 structure
str(cars)

# 从内存中删除某个变量
rm(pf)

# 查看pf的所有变量
> names(pf)
 [1] "userid"                "age"                  
 [3] "dob_day"               "dob_year"             
 [5] "dob_month"             "gender"               
 [7] "tenure"                "friend_count"         
 [9] "friendships_initiated" "likes"                
[11] "likes_received"        "mobile_likes"         
[13] "mobile_likes_received" "www_likes"            
[15] "www_likes_received" 
# 安装 R包ggplot2
install.packages('ggplot2')
# 引入 ggplot2
library(ggplot2)

# 获得其他主题
install.packages('ggthemes', dependencies = TRUE) 
library(ggthemes)
# 使用 theme_minimal(),字体大小为 24
theme_set(theme_minimal(24)) 

# 绘制直方图
qplot(x=dob_day, data=pf)

qplot(x=dob_day, data=pf) + scale_x_continuous(breaks = 1:31)

猜你喜欢

转载自blog.csdn.net/guo_ya_nan/article/details/81089909