Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/68.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何避免R中的循环?_R_Loops_Apply - Fatal编程技术网

如何避免R中的循环?

如何避免R中的循环?,r,loops,apply,R,Loops,Apply,我知道在R中,最好尽可能避免循环。在这方面,我想执行下面代码的功能,但不使用嵌套循环 循环检查向量我想找到的东西的f第个元素是否存在于要搜索的东西的I第行中。例如,当i和f都为1时,代码检查john的行中是否存在“vocals”。由于“vocals”出现在john的行中,因此名称和乐器被添加到vectorsinstrument和name中。当两个循环完成时,这两个向量可以组合在data.frame中 我知道R中有apply()函数族,但我不知道它们是否可以在这种情况下使用。有人有什么有用的提示或

我知道在
R
中,最好尽可能避免循环。在这方面,我想执行下面代码的功能,但不使用嵌套循环

循环检查向量
我想找到的东西
f
第个元素是否存在于
要搜索的东西的
I
第行中。例如,当
i
f
都为1时,代码检查
john
的行中是否存在
“vocals”
。由于
“vocals”
出现在
john
的行中,因此名称和乐器被添加到vectors
instrument
name
中。当两个循环完成时,这两个向量可以组合在
data.frame

我知道R中有
apply()
函数族,但我不知道它们是否可以在这种情况下使用。有人有什么有用的提示或建议吗

instrument<-c()

name<-c()

things_I_want_to_find<-c("vocals","drums","rhythm guitar","bass")

thing_to_be_searched<-
data.frame(
id=c("john","paul","george","ringo"),
a=c("vocals","bass","rhythm guitar","oboe"),
b=c("vocals","basoon","piano","clarinet"),
c=c("violin","vocals","french horn","drums"))
for(f in 1:length(things_I_want_to_find))
{
  for(i in 1:nrow(thing_to_be_searched))
  {
    n<-which(thing_to_be_searched[i,]==things_I_want_to_find[f])
    if(length(n)>0)
    {
      instrument<-c(instrument,as.character(thing_to_be_searched[i,][n][,1][1]))
      name<-c(name,as.character(thing_to_be_searched$id[i]))
    }

    
  }
}

desired_output<-data.frame(name=name,instrument=instrument)
desired_output
    name    instrument
1   john        vocals
2   paul        vocals
3  ringo         drums
4 george rhythm guitar
5   paul          bass
仪器
输出

# A tibble: 5 x 2
  id     value        
  <chr>  <chr>        
1 john   vocals       
2 paul   bass         
3 paul   vocals       
4 george rhythm guitar
5 ringo  drums 
#一个tible:5 x 2
id值
1约翰声乐
2保罗·巴斯
3保罗主唱
4乔治节奏吉他
5林戈鼓

使用
dplyr
-

library(dplyr)

thing_to_be_searched %>%
  group_by(id) %>%
  summarise(instrument = things_I_want_to_find[things_I_want_to_find %in% cur_data()]) %>%
  ungroup

#   id     instrument   
#  <chr>  <chr>        
#1 george rhythm guitar
#2 john   vocals       
#3 paul   vocals       
#4 paul   bass         
#5 ringo  drums        
库(dplyr)
要搜索的对象%>%
分组依据(id)%>%
总结(instrument=things\u I\u want\u find[things\u I\u want\u find%在%cur\u data()中])%>%
解组
#身份证文书
#            
#1乔治节奏吉他
#2约翰声乐
#3保罗主唱
#4保罗·巴斯
#5林戈鼓

使用
基本R
重塑
(使用
R 4.1.0

要搜索的东西|>
集合名(c('id',paste0('a',1:3))|>
重塑(方向='长',变化=2:4,sep=“”)|>
子集(我想找到的东西百分比中的a%,选择=c(id,a))|>
唯一()

`row.names今天我学到了:在BaseR中很好地使用管道!BaseR惊人的新特性。刚刚知道R4.1.0发布了(由您的回答通知):P
library(dplyr)

thing_to_be_searched %>%
  group_by(id) %>%
  summarise(instrument = things_I_want_to_find[things_I_want_to_find %in% cur_data()]) %>%
  ungroup

#   id     instrument   
#  <chr>  <chr>        
#1 george rhythm guitar
#2 john   vocals       
#3 paul   vocals       
#4 paul   bass         
#5 ringo  drums        
thing_to_be_searched |>
   setNames(c('id', paste0('a', 1:3))) |>
   reshape(direction = 'long',  varying  = 2:4, sep="") |>
   subset(a %in% things_I_want_to_find, select = c(id, a)) |>
   unique() |>
   `row.names<-`(NULL)
#      id             a
#1   john        vocals
#2   paul          bass
#3 george rhythm guitar
#4   paul        vocals
#5  ringo         drums