[英]Using “apply” functions across multiple data frames
我在多个数据帧之间使用Apply函数(我认为这是执行以下操作的正确方法)时遇到问题。
一些示例数据(3个不同的数据帧,但我正在处理的问题超过50个):
biz <- data.frame(
country = c("england","canada","australia","usa"),
businesses = sample(1000:2500,4))
pop <- data.frame(
country = c("england","canada","australia","usa"),
population = sample(10000:20000,4))
restaurants <- data.frame(
country = c("england","canada","australia","usa"),
restaurants = sample(500:1000,4))
这是我最终想要做的:
1)根据所包含的变量,从最大到最小对饮食数据帧进行排序
dataframe <- dataframe[order(dataframe$VARIABLE,)]
2)然后创建一个向量变量,该变量为我提供每个变量的排名
dataframe$rank <- 1:nrow(dataframe)
3)然后创建另一个数据框,该数据框的一列国家/地区和每个感兴趣变量的排名与其他列一样。 看起来像(这里的排名不是真实的):
country.rankings <- structure(list(country = structure(c(5L, 1L, 6L, 2L, 3L, 4L), .Label = c("brazil",
"canada", "england", "france", "ghana", "usa"), class = "factor"),
restaurants = 1:6, businesses = c(4L, 5L, 6L, 3L, 2L, 1L),
population = c(4L, 6L, 3L, 2L, 5L, 1L)), .Names = c("country",
"restaurants", "businesses", "population"), class = "data.frame", row.names = c(NA,
-6L))
所以我猜想有一种方法可以将这些数据帧中的每一个都放到一个列表中,例如:
lib <- c(biz, pop, restaurants)
然后对其进行一次不适用于1)排序,2)创建等级变量和3)为每个国家/地区的每个变量(企业数量,人口规模,餐厅数量)创建排名矩阵或数据框架。 我遇到的问题是,当我尝试按变量排序时,编写lapply函数对每个数据帧进行排序会遇到问题:
sort <- lapply(lib,
function(x){
x <- x[order(x[,2]),]
})
返回错误信息:
Error in `[.default`(x, , 2) : incorrect number of dimensions
因为我正在尝试将列标题应用于列表。 但是,当每个数据框的变量名称都不同时,我还要如何解决这个问题(但要记住,国家名称是一致的)
(也很想知道如何使用plyr来使用它)
理想情况下,我会为此推荐data.table
。 但是,这是使用data.frame
的快速解决方案,请尝试以下操作:
步骤1:创建所有data.frames的列表
varList <- list(biz,pop,restaurants)
步骤2:将所有内容合并到一个data.frame中
temp <- varList[[1]]
for(i in 2:length(varList)) temp <- merge(temp,varList[[i]],by = "country")
第三步:获得排名:
cbind(temp,apply(temp[,-1],2,rank))
您可以根据需要删除不想要的列!
cbind(temp[,1:2],apply(temp[,-1],2,rank))[,-2]
希望这可以帮助!!
totaldatasets <- c('biz','pop','restaurants')
totaldatasetslist <- vector(mode = "list",length = length(totaldatasets))
for ( i in seq(length(totaldatasets)))
{
totaldatasetslist[[i]] <- get(totaldatasets[i])
}
totaldatasetslist2 <- lapply(
totaldatasetslist,
function(x)
{
temp <- data.frame(
country = totaldatasetslist[[i]][,1],
countryrank = rank(totaldatasetslist[[i]][,2])
)
colnames(temp) <- c('country', colnames(x)[2])
return(temp)
}
)
Reduce(
merge,
totaldatasetslist2
)
输出-
country businesses population restaurants
1 australia 3 3 3
2 canada 2 2 2
3 england 1 1 1
4 usa 4 4 4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.