作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
现在我有一个数据框和两个列表,每个列表都包含一些数据框名称,我需要添加新列,其中包含数据框内每一列的排名问题是我必须对 Deslist1
中的列进行排名降序方式和 colnames 与 Asclist1
匹配的列以升序方式,因此最终需要的输出类似于
我尝试了 any(
和 within
但它不起作用tr<-any(Asclist1 %in% DF1
同样,我的问题不是如何添加包含排名的新列我的问题是如何根据列表元素进行排名,如您所见 Asclist1
包含 DF1
中不存在的元素列
DF1 <- data.frame("name" = c("john", "adam", "leo", "lena", "Di"),
"sex" = c("m", "m", "m", "f", "f"),
"age" = c(99, 46, 23, 54, 23),
"grade" = c(96, 46, 63, 54, 23),
"income" = c(59, 36, 93, 34, 23),
"score" = c(99, 46, 23, 54, 23))
print(DF1)
Asclist1<-list("score","income","spending")
Asclist1
Deslist2<-list("age","grade")
Deslist2
更新----代码1
library(readr)
library(tidyr)
library(purrr)
library(rlang)
library(glue)
library(dplyr)
library(miscTools)
library(matrixStats)
library(shiny)
library(reshape2)
library(dplyr)
hotdrinks<-list("tea","green tea")
juices<-list("orange","mango")
energydrinks<-list("powerhorse","redbull")
drinks<-list("hotdrinks"=hotdrinks,"juices"=juices,"energydrinks"=energydrinks)
biscuits<-list("loacker","tuc")
choc<-list("aftereight","lindt")
gum<-list("trident","clortes")
sweets<-list("gum"=gum,"biscuits"=biscuits,"choc"=choc)
all_products<-list("sweets"=sweets,"drinks"=drinks)
mt<-melt(all_products)
mt2<-mt%>%mutate("Price"=c(23,34,23,23,54,32,45,23,12,56,76,43),
"Quantity"=c(10,20,26,22,51,52,45,23,12,56,76,43),
"amount"=c(23,34,23,23,54,32,45,23,12,56,76,43))
t1<-mt2[,c(4,3,1,5,6,7)]
t1
colnames(t1)<-c("CAT","PN","SP","Quantity","Price","amount")
t2<-list(unique(t1$CAT))
t2
QL<-c("Quantity","Price")
QD<-c("Quantity","amount")
QS<-c("amount","Price")
all <- list("drinks"=drinks, "sweets"=sweets)
FCX<-data.frame("sbo"=c("w","q","a"),
"Quantity_fcx"=c(3,2,5),
"Price_fcx"=c(7,8,5),
"amount_fcx"=c(4,7,3)
)
#DF1<-Y
DF1 <- t1
DF1
#print(DF1)
DFCXL<-list(colnames(DF1[-c(1:3)]))
DFCXL
DFCX1<-lapply(DFCXL, paste0, "_fcx")
DFCX1
DFCXM<-colMeans(FCX[,unlist(DFCX1)],na.rm = FALSE)
DFCXM
DFCXMd<-colMedians(data.matrix(FCX[,unlist(DFCX1)]),na.rm = FALSE )
DFCXMddf<-as.data.frame(t(DFCXMd))
DFCXMddf
DFCX1l<-as.list(DFCX1)
colnames(DFCXMddf)<-unlist(DFCX1l)
DFCXMddf
#median repeated tibble
rDFCXMddf<-DFCXMddf[rep(seq_len(nrow(DFCXMddf)), each = nrow(DF1)), ]
rDFCXMddf
DFCX<-data.frame(t(DFCXM))
DFL<-as.vector(colnames(DF1))
DFL
DFCX
#mean repeated tibble
rDFCX<-DFCX[rep(seq_len(nrow(DFCX)), each = nrow(DF1)), ]
#rDFCX
#ascending rank form smallest to largest where the smallest is the most competitive
Asclist1<-list("Quantity","Price")
#Asclist1
#descending rank form largest to smallest where the largest is the most competitive
Deslist2<-list("xyz","amount")
#Deslist2
#DF3 contains orginal dataframe with rank for each column descending & ascending
DF3<-
DF1 %>% mutate_if(grepl(paste(Deslist2, collapse = "|"), names(.)), list(rank=~rank(-.))) %>%
mutate_if(grepl(paste(Asclist1, collapse = "|"), names(.)), list(rank=~rank( .)))
DF3
#DF4 contains only determinants columns
DF4<-DF3%>%select(-one_of(DFL))
DF4
#DF5 contains all deterements with their ranks columns
DF5<-cbind(rDFCX,DF4)
DF5
#getting final rank for each column based on multiplying CX columns "weight" * normal rank to get weighted ranking
dynamic_mutate = function(DF5,
col_names = gsub("(.*)_\\w+$", "\\1", names(DF5)),
expression = "({x}_rank*{x}_fcx)",
prefix = "FINAL"){
name_list = col_names %>% unique() %>% as.list()
expr_list = name_list %>% lapply(function(x) parse_quosure(glue(expression))) %>%
setNames(paste(prefix, name_list, sep = "_"))
DF5 %>% mutate(!!!expr_list)}
DF6<-DF5 %>% dynamic_mutate()
#DF6
#getting mean for ranks
DFL2<-as.vector(colnames(DF5))
DF7<-DF6%>%select(-one_of(DFL2))
#DF7
#final limit ranking
DF8<-mutate(DF7,fnl_scr=rowMeans(DF7))
#DF8
#final rank
Ranking<-rank(DF8$fnl_scr)
#Ranking
#final dataframe
DF9<-as_tibble(cbind(DF1,Ranking))
DF9
代码 2
library(readr)
library(tidyr)
library(purrr)
library(rlang)
library(glue)
library(dplyr)
library(miscTools)
library(matrixStats)
library(shiny)
library(reshape2)
library(dplyr)
hotdrinks<-list("tea","green tea")
juices<-list("orange","mango")
energydrinks<-list("powerhorse","redbull")
drinks<-list("hotdrinks"=hotdrinks,"juices"=juices,"energydrinks"=energydrinks)
biscuits<-list("loacker","tuc")
choc<-list("aftereight","lindt")
gum<-list("trident","clortes")
sweets<-list("gum"=gum,"biscuits"=biscuits,"choc"=choc)
all_products<-list("sweets"=sweets,"drinks"=drinks)
mt<-melt(all_products)
mt2<-mt%>%mutate("Price"=c(23,34,23,23,54,32,45,23,12,56,76,43),
"Quantity"=c(10,20,26,22,51,52,45,23,12,56,76,43),
"amount"=c(23,34,23,23,54,32,45,23,12,56,76,43))
t1<-mt2[,c(4,3,1,5,6,7)]
t1
colnames(t1)<-c("CAT","PN","SP","Quantity","Price","amount")
t2<-list(unique(t1$CAT))
t2
QL<-c("Quantity","Price")
QD<-c("Quantity","amount")
QS<-c("amount","Price")
all <- list("drinks"=drinks, "sweets"=sweets)
FCX<-data.frame("sbo"=c("w","q","a"),
"Quantity_fcx"=c(3,2,5),
"Price_fcx"=c(7,8,5),
"amount_fcx"=c(4,7,3)
)
#DF1<-Y
DF1 <- t1
DF1
#print(DF1)
DFCXL<-list(colnames(DF1[-c(1:3)]))
DFCXL
DFCX1<-lapply(DFCXL, paste0, "_fcx")
DFCX1
DFCXM<-colMeans(FCX[,unlist(DFCX1)],na.rm = FALSE)
DFCXM
DFCXMd<-colMedians(data.matrix(FCX[,unlist(DFCX1)]),na.rm = FALSE )
DFCXMddf<-as.data.frame(t(DFCXMd))
DFCXMddf
DFCX1l<-as.list(DFCX1)
colnames(DFCXMddf)<-unlist(DFCX1l)
DFCXMddf
#median repeated tibble
rDFCXMddf<-DFCXMddf[rep(seq_len(nrow(DFCXMddf)), each = nrow(DF1)), ]
rDFCXMddf
DFCX<-data.frame(t(DFCXM))
DFL<-as.vector(colnames(DF1))
DFL
DFCX
#mean repeated tibble
rDFCX<-DFCX[rep(seq_len(nrow(DFCX)), each = nrow(DF1)), ]
#rDFCX
#ascending rank form smallest to largest where the smallest is the most competitive
Asclist1<-list("Quantity","Price","amount")
#Asclist1
#descending rank form largest to smallest where the largest is the most competitive
Deslist2<-list("xyz")
#Deslist2
#DF3 contains orginal dataframe with rank for each column descending & ascending
DF3<-
DF1 %>% mutate_if(grepl(paste(Deslist2, collapse = "|"), names(.)), list(rank=~rank(-.))) %>%
mutate_if(grepl(paste(Asclist1, collapse = "|"), names(.)), list(rank=~rank( .)))
DF3
#DF4 contains only determinants columns
DF4<-DF3%>%select(-one_of(DFL))
DF4
#DF5 contains all deterements with their ranks columns
DF5<-cbind(rDFCX,DF4)
DF5
#getting final rank for each column based on multiplying CX columns "weight" * normal rank to get weighted ranking
dynamic_mutate = function(DF5,
col_names = gsub("(.*)_\\w+$", "\\1", names(DF5)),
expression = "({x}_rank*{x}_fcx)",
prefix = "FINAL"){
name_list = col_names %>% unique() %>% as.list()
expr_list = name_list %>% lapply(function(x) parse_quosure(glue(expression))) %>%
setNames(paste(prefix, name_list, sep = "_"))
DF5 %>% mutate(!!!expr_list)}
DF6<-DF5 %>% dynamic_mutate()
#DF6
#getting mean for ranks
DFL2<-as.vector(colnames(DF5))
DF7<-DF6%>%select(-one_of(DFL2))
#DF7
#final limit ranking
DF8<-mutate(DF7,fnl_scr=rowMeans(DF7))
#DF8
#final rank
Ranking<-rank(DF8$fnl_scr)
#Ranking
#final dataframe
DF9<-as_tibble(cbind(DF1,Ranking))
DF9
最佳答案
另一种选择是使用 map
通过创建 1, -1s 的列来同时执行此操作
library(dplyr)
library(tidyr)
library(purrr)
library(stringr)
tibble(col1 = list(Asclist1, Deslist2), col2 = c(1, -1)) %>%
unnest_longer(col1) %>%
group_split(col2) %>%
map_dfc(~ DF1 %>%
mutate(tmp = first(.x$col2)) %>%
select(one_of(.x$col1), tmp) %>%
transmute_at(vars(-tmp), list(rank = ~rank(tmp * .)))) %>%
bind_cols(DF1, .)
# name sex age grade income score age_rank grade_rank income_rank score_rank
#1 john m 99 96 59 99 1.0 1 4 5.0
#2 adam m 46 46 36 46 3.0 4 3 3.0
#3 leo m 23 63 93 23 4.5 2 5 1.5
#4 lena f 54 54 34 54 2.0 3 2 4.0
#5 Di f 23 23 23 23 4.5 5 1 1.5
#Warning message:
#Unknown columns: `spending`
它还会通知未知列作为警告
如果只有一个列带有transmute_at
,它不会将list
中的名称添加为后缀。为了绕过它,我们可以使用 rename_if
f1 <- function(dat) {
nm1 <- setdiff(names(dat), "tmp")
n1 <- length(nm1)
dat %>%
transmute_at(vars(-tmp), list(rank = ~rank(tmp * .))) %>%
rename_if(rep(n1 == 1, n1), ~ str_c(nm1, "_", .))
}
tibble(col1 = list(Asclist1, Deslist2), col2 = c(1, -1)) %>%
unnest_longer(col1) %>%
group_split(col2) %>%
map_dfc(~ DF1 %>%
mutate(tmp = first(.x$col2)) %>%
select(one_of(.x$col1), tmp) %>%
f1(.)) %>%
bind_cols(DF1, .)
# CAT PN SP Quantity Price amount amount_rank Quantity_rank Price_rank
# 1 sweets gum trident 23 10 23 9.5 3.5 1
# 2 sweets gum clortes 34 20 34 6.0 7.0 3
# 3 sweets biscuits loacker 23 26 23 9.5 3.5 6
# 4 sweets biscuits tuc 23 22 23 9.5 3.5 4
# 5 sweets choc aftereight 54 51 54 3.0 10.0 9
# 6 sweets choc lindt 32 52 32 7.0 6.0 10
# 7 drinks hotdrinks tea 45 45 45 4.0 9.0 8
# 8 drinks hotdrinks green tea 23 23 23 9.5 3.5 5
# 9 drinks juices orange 12 12 12 12.0 1.0 2
# 10 drinks juices mango 56 56 56 2.0 11.0 11
# 11 drinks energydrinks powerhorse 76 76 76 1.0 12.0 12
# 12 drinks energydrinks redbull 43 43 43 5.0 8.0 7
关于r - 在 r 中,如何根据列表元素对数据框的一些列降序排列,而其他列升序排列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59226342/
序 大家好呀,我是summo,这次来写写我在上班空闲(摸鱼)的时候做的一个小网站的事。去年阿里云不是推出了个活动嘛,2核2G的云服务器一年只要99块钱,懂行的人应该知道这个价格在业界已经是非常良心了
我尝试根据给定的级别顺序(BFS 顺序)构造 BST。我知道这是可能的,但我不知道我该怎么写。问题是我必须使用 BFS 序列。所以,我不能在这里使用递归,我必须迭代地编写我的程序......我发现这有
我是一名优秀的程序员,十分优秀!