R:如果基于列表/向量的多个条件的语句

时间:2017-09-22 03:26:10

标签: r loops if-statement

假设我有一个n向量,我想根据存储在另一个向量/列表中的多个条件应用if条件,如何循环并将名称分配给我的原始向量?

set.seed(1234)

# Data
b <- rpois(100, 4)

# Condition
a <- 1:10
names(a) <- paste0(seq(10,100, by = 10),'%')

2 个答案:

答案 0 :(得分:1)

这是你想要的吗?

set.seed(1234)

# n-vector
b <- rpois(100, 4)

#  multiple conditions stored in another vector/list 
a <- 1:10
names(a) <- paste0(seq(10,100, by = 10),'%')

# loop and assign the names to the original vector
for(i in unique(a)){
  b <- ifelse(b==i,names(a[a==i]),b)
}
b
  [1] "20%"  "40%"  "40%"  "40%"  "60%"  "50%"  "0"    "20%"  "50%"  "40%"  "50%"  "40%"  "30%"  "70%"  "30%"  "60%"  "30%"  "30%" 
 [19] "20%"  "20%"  "30%"  "30%"  "20%"  "10%"  "20%"  "60%"  "40%"  "70%"  "60%"  "10%"  "40%"  "30%"  "30%"  "40%"  "20%"  "50%" 
 [37] "20%"  "30%"  "100%" "60%"  "40%"  "50%"  "30%"  "40%"  "30%"  "40%"  "50%"  "40%"  "30%"  "50%"  "10%"  "30%"  "50%"  "40%" 
 [55] "20%"  "40%"  "40%"  "50%"  "20%"  "60%"  "60%"  "10%"  "30%"  "0"    "30%"  "50%"  "30%"  "40%"  "10%"  "40%"  "20%"  "70%" 
 [73] "0"    "50%"  "10%"  "40%"  "30%"  "10%"  "30%"  "50%"  "70%"  "40%"  "20%"  "40%"  "20%"  "70%"  "30%"  "30%"  "20%"  "70%" 
 [91] "20%"  "70%"  "20%"  "20%"  "20%"  "40%"  "30%"  "10%"  "30%"  "50%"

答案 1 :(得分:0)

如果我们需要基于'a'的命名match向量,请使用names(b) <- names(a)[match(b, a)] names(b) #[1] "20%" "40%" "40%" "40%" "60%" "50%" NA "20%" "50%" "40%" "50%" "40%" "30%" "70%" "30%" #[16] "60%" "30%" "30%" "20%" "20%" "30%" "30%" "20%" "10%" "20%" "60%" "40%" "70%" "60%" "10%" #[31] "40%" "30%" "30%" "40%" "20%" "50%" "20%" "30%" "100%" "60%" "40%" "50%" "30%" "40%" "30%" #[46] "40%" "50%" "40%" "30%" "50%" "10%" "30%" "50%" "40%" "20%" "40%" "40%" "50%" "20%" "60%" #[61] "60%" "10%" "30%" NA "30%" "50%" "30%" "40%" "10%" "40%" "20%" "70%" NA "50%" "10%" #[76] "40%" "30%" "10%" "30%" "50%" "70%" "40%" "20%" "40%" "20%" "70%" "30%" "30%" "20%" "70%" #[91] "20%" "70%" "20%" "20%" "20%" "40%" "30%" "10%" "30%" "50%"

names

请注意,'b'中的元素不在'a'中。目前尚不清楚该元素的match应该是什么。默认情况下,NA会返回这些元素的NA,并且该元素的名称将为WITH table_name AS ( SELECT '1' AS code, ' ' AS code_ct FROM dual UNION ALL SELECT '2', ' ' FROM dual UNION ALL SELECT '11', '1' FROM dual UNION ALL SELECT '12', '1' FROM dual UNION ALL SELECT '111', '11' FROM dual UNION ALL SELECT '112', '11' FROM dual UNION ALL SELECT '1111', '111' FROM dual UNION ALL SELECT '1112', '111' FROM dual UNION ALL SELECT '1113', '111' FROM dual UNION ALL SELECT '1114', '111' FROM dual UNION ALL SELECT '1115', '111' FROM dual UNION ALL SELECT '21', '2' FROM dual UNION ALL SELECT '211', '21' FROM dual UNION ALL SELECT '22', '2' FROM dual ) --Current query that give expected output SELECT * FROM table_name START WITH code IN ('1112', '1114', '1115', '211') CONNECT BY PRIOR code_ct = code INTERSECT SELECT * FROM table_name START WITH code = '11' CONNECT BY PRIOR code = code_ct; /* --test improving query SELECT SUBSTR(SYS_CONNECT_BY_PATH(code , '_'), 2) AS path FROM table_name WHERE code = '11' START WITH code IN ('1112', '1114', '1115', '221') CONNECT BY PRIOR code_ct = code; */ 。如果我们需要改变它,我们可以做到这一点