R语言处理数据遇到的几个问题
用R语言处理一系列轨迹数据(点由经纬度表示)时,用到了了几个常见的函数。
1.去除带有NA的行,采用complete.cases();
1 | temp <- temp[ complete.cases (temp), ]; |
其中temp为你的数据,这样就可以将带有NA的行全部删除。
2.获得数据的行数,采用nrow();
1 | n_user <- nrow (user_order); |
3.建立一个长度任意的空向量,采用vector(mode = "numeric", length = N);
1 | radius <- vector (mode = "numeric" , length = n_user); |
4.将数据按照某一列/行进行排序,采用order();
1 | user_order <- user_7day_user11[ order (user_7day_user11[,1]), ]; |
例如,这个是将user_7day_user11这个数据,按照第一列的数据进行排序。
5.建立一个维数自定义的矩阵,采用matrix(x, nrow=NROW, ncol=NCOL);
1 | temp <- t ( matrix ( as.vector ( t (temp)), nrow = 2, ncol = n_day*24)); |
这里还用到了t()函数对矩阵进行转置。
6.超级有用的距离计算!根据经纬度计算距离,有两种,分别在“geosphere”和“SoDA”这两个包中,两者都适用于向量的计算。
distm() in package geosphere
geoDist() in package SoDA
(1)geoDist(lat1, lon1, lat2, lon2, NAOK = TRUE, DUP = TRUE),得到前面的lat1,lon1与后面的lat2,lon2间的距离;
(2)distm(x,y),x为lon,lat的数据,y也为lon,lat数据,注意他的经纬度顺序与上面不一样,得到距离矩阵。
1 | radius[i] <- mean ( distm (temp_location, rg[i,])); |
这里的temp_location为XX行2列的数组,分别为一组点的longitude与latitude;rg为这系列点的重心(经纬度的均值),所以我这里求的是回旋半径(gyration)。
7.终于尝试用apply(x, row/col, function),真的很好用。
这个函数的意思就是对数据x进行操作;第二个参数row/col代表,若为1,则是对行进行操作,若为2,则是对列进行操作;第三个参数就是你要用的函数。
1 | rg[i,] <- apply (temp_location, 2, mean); |
这里用它求一系列点的重心。
8.系统时间函数,Sys.time()。
9.对行进行求和,采用rowSums(data) 。
1 | count_location[j] <- sum ( rowSums (temp_location-temp) == 0); |
这里是完成了temp_location与temp这两个矩阵行对应相等的行数。
10.最后贴几段计算移动熵、移动重心、移动半径、出现地点次数的代码。
% 读入轨迹和用户数据,并进行排序(按照imei进行排序) tra_7day_user11 <- read.csv ( "/Users/guosh/Documents/Courses/spatial data mining/exportData/tra_7day_user11.csv" , sep = "," , header = FALSE ); user_7day_user11 <- read.csv ( "/Users/guosh/Documents/Courses/spatial data mining/exportData/user_7day_user11.csv" , sep = "," , header = FALSE ); tra_order <- tra_7day_user11[ order (tra_7day_user11[,2]), ]; user_order <- user_7day_user11[ order (user_7day_user11[,1]), ]; % 计算移动熵 start_time <- Sys.time (); n_user <- nrow (user_order); count <- 1; entropy <- cbind (user_order, vector (mode = "numeric" , length = n_user)); for (i in 1:n_user){ count0 <- count; n_day <- user_order[i,2]; count <- count0+n_day; temp <- tra_order[count0:(count-1), 3:50]; temp <- t ( matrix ( as.vector ( t (temp)), nrow = 2, ncol = n_day*24)); temp <- temp[ complete.cases (temp), ]; % 去掉 NA 值 location <- unique (temp); n_location <- nrow (location); count_location <- vector (mode = "numeric" , length = n_location); for (j in 1:n_location){ temp_location <- data.frame (lon = vector (mode = "numeric" , length = nrow (temp)), lat = vector (mode = "numeric" , length = nrow (temp))); temp_location$lon <- location[j,1]; temp_location$lat <- location[j,2]; count_location[j] <- sum ( rowSums (temp_location-temp) == 0); % 判断某一行出现的次数 } p_location <- count_location/ nrow (temp); entropy[i,3] <- - sum (p_location* log (p_location,2)); } end_time <- Sys.time (); duration <- end_time-start_time; % 实验花费了2.10552小时 % 计算移动半径 % 统计用户出现的不同地点的个数 start_time <- Sys.time (); rg <- data.frame (lon = vector (mode = "numeric" , length = n_user), lat = vector (mode = "numeric" , length = n_user)); radius <- vector (mode = "numeric" , length = n_user); place <- vector (mode = "numeric" , length = n_user); freq <- vector (mode = "numeric" , length = n_user); count <- 1; for (i in 1:n_user){ count0 <- count; n_day <- user_order[i,2]; count <- count0+n_day; temp <- tra_order[count0:(count-1), 3:50]; temp <- t ( matrix ( as.vector ( t (temp)), nrow = 2, ncol = n_day*24)); temp <- temp[ complete.cases (temp), ]; temp_location <- unique (temp); n_location <- nrow (temp_location); % 计算用户出现的地点的个数和频率 place[i] <- n_location; freq[i] <- n_location/n_day; % 计算用户的移动重心和移动半径 rg[i,] <- apply (temp_location, 2, mean); radius[i] <- mean ( distm (temp_location, rg[i,])); } end_time <- Sys.time (); duration <- end_time-start_time; |
补充几个常用函数:
1. 产生一定范围内的随机数:runif(100,0,2),代表产生100个0-2范围内的随机数。如果想要得到整数,可以用floor(runif(100,1,200))。
2. 查看已经下载的包:library()。
3. 查看下载的包及其版本:installed.packages()[,c("Package", "Version")]。
4.用R实现统计数据框或者矩阵不重复的行其出现的次数。需要用到的包:“dplyr”。
1 2 | temp <- matrix ( as.vector ( t (temp)), nrow = n_day*24, ncol = 2, byrow = TRUE , dimnames = list ( NULL , c ( 'lon' , 'lat' ))); count_location <- group_by ( as.data.frame (temp), lon, lat) %>% summarise (n = n ()); |
如上所示,temp为一个矩阵,有2列,列名为'lon', 'lat',下面的group by代码即为统计每一行出现的次数。结果大致为:
lon, lat, n
XX,XX,XX
这样的形式,n列为不重复的行出现的次数。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)