R语言现学现做

目标:对文件进行数据分析
 
 
1.环境配置

https://blog.csdn.net/weixin_42032429/article/details/83095899

2.连接文件

安装xlsx软件包

安装顺序

https://www.cnblogs.com/chenlu-vera/p/9334142.html

install.packages("rJava") 
install.packages("xlsxjars") 
install.packages("xlsx")

验证是否安装成功

any(grepl("xlsx",installed.packages()))

尝试调用xlsx包失败,先安装了xlsx,后rJava,导致loadNamespace()里算'rJava'时.onLoad失败了

 

操作系统的版本是win10, 64bit,我系统安装的jdk1.8,是32bit的,而使用的R版本是64位的;R版本与jre版本不兼容导致;更改R版本位32位的即可!

RStudio中,Tools –> Global Options –General –> R version Change一下(如果安装R时没勾选32bit的,就重新安装一下)

然后执行

library("xlsx")

接下来使用read.xlsx()函数来读取excel数据

导入文件到RStudio中

file->import dataset->from excel

安装完依赖包就选取导入数据的目录

导入后

可以看到数据已经保存到ABPrices中了

查看数据

ABPrices

3.基本操作

(1)查看表格维数

dim(ABPrices)

(2)查看表格的数据结构

str(ABPrices)

(3)获取描述性统计

summary(ABPrices)

(4)缺失值统计

统计没有数据的表格数量

sum(is.na(ABPrices))

(5)将数据表中的用户ID设置为索引

rownames(ABPrices)=ABPrices$id

(6)查看数据表

head(ABPrices)

(7)提取数据表前两行

ABPrices[1:2,]

(8)提取特定用户ID信息

ABPrices['5441']

(9)提取多个用户ID信息

ABPrices[c('5099','7322'),]

(10)提取多列信息

head(ABPrices[1:2])

(11)提取指定列信息

head(ABPrices['host_name'])

(12)提取指定行列信息

ABPrices[1:2,5:6]

(13)提取在指定用户的指定信息

ABPrices['7322','host_name']

(14)提取金额最大值的行信息

ABPrices[which.max(ABPrices$price),]

(15)提取顾客数最小值的行信息

ABPrices[which.min(ABPrices$number_of_reviews),]

(16)找到房租金额最大的那个值

ss=min(ABPrices$price)
print(ss)

(17)找到房租金额平均值

print(mean(ABPrices$price))

 (18)去除0值后找到房租金额平均值

print(mean(ABPrices$price[ABPrices$price!=0]))

 (19)计算众数

自定义函数

FindMode <- function(x) {
    ux <- unique(x)
    ux[which.max(tabulate(match(x, ux)))]
}

使用函数

FindMode(ABPrices$price[ABPrices$price!=0])

 (20)标准误差

标准偏差除以样本大小的平方根

std <- function(x) sd(x)/sqrt(length(x))

调用

std(ABPrices$price[ABPrices$price!=0])

 (21)标准差

sd(ABPrices$price[ABPrices$price!=0])

(22)方差

var(ABPrices$price[ABPrices$price!=0])

(23)计算偏度和峰度

mean(((newPrice-mean(newPrice))/sd(newPrice))^3)

或者

install.packages("EnvStats")
library("EnvStats")
skewness(x) #EnvStats包中计算偏度系数函数
kurtosis(x) #EnvStats包中计算峰度系数函数

(24)统计房产数量最多的房屋主

建立表格,使用table函数统计host_id出现次数,并将host_id与频率组合为一张表

freq <- data.frame(table(ABPrices$host_id))

将该表的统计数组剥离出来按照由大到小排序

tmp[order(tmp,decreasing = TRUE)]

并赋值给新数组

tmp2<-tmp[order(tmp,decreasing = TRUE)]

根据房屋数量输出对应host_id

freq$Var1[freq$Freq==tmp2[1]]

 中位数

median(x, na.rm = FALSE)
4.简单统计

(1)统计不同地区的房屋数量

#下载依赖包
install.packages("dplyr")
install.packages("ggplot2")

#加载依赖包
library(dplyr)
library(ggplot2)

#统计各区住房数量
freq <- data.frame(table(ABPrices$neighbourhood_group))

plot <- ggplot(data = freq, mapping = aes(x = reorder(Var1, -Freq),y = Freq)) + geom_bar(stat = 'identity', fill = 'lightcoral') + theme(axis.text.x  = element_text(angle = 45, vjust = 0.5)) + xlab('Areas') + ylab('Numbers')+ coord_flip()

plot

 (2)显示地图上的点

使用leaflet地图包

install.packages("leaflet")
library("leaflet")

添加标记点

 df = data.frame(
     lat = ABPrices$latitude,
     lng = ABPrices$longitude,
     size = 1
 )
 m<-leaflet(data=df)
 m<-addTiles(m)
 addCircleMarkers(m,lng=~lng,lat=~lat,radius = ~size, fill = TRUE)

原文地址:https://www.cnblogs.com/ak918xp/p/13889707.html