Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/83.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_List Comparison - Fatal编程技术网

根据R中的列表计算的值的标签

根据R中的列表计算的值的标签,r,list-comparison,R,List Comparison,样本数据: #Referece set Newyork <- c("ant", "bat", "cat", "dog", "unicorn", "camel", "snake", "monkey", "donkey") Tokyo <- c("unicorn") Yokohama <- c("ant", "bat", "cat", "dog") Chicago <- c("bird", "ant", "bat", "cat", "bear", "dog", "sna

样本数据:

#Referece set
Newyork <- c("ant", "bat", "cat", "dog", "unicorn", "camel", "snake", "monkey", "donkey")
Tokyo <-    c("unicorn")
Yokohama <- c("ant", "bat", "cat", "dog")
Chicago <- c("bird", "ant", "bat", "cat", "bear", "dog", "snake", "monkey", "mouse", " donkey", "octopus", "camel")
Nashville <-c("ant", "bat", "octopus") 
DC <-   c("ant", "dog", "cat", "bird")
Boston <-   c("ant", "bird", "cat", "bear", "camel", "snake", "mouse", "octopus")

#query set
Elendel <- c("wolf", "dog" ,"ant")

#combining References
Refcities <- list(Boston, Chicago, DC, Nashville, Newyork, Tokyo, Yokohama)
如何将参考城市中的数据相加,并根据结果按数字顺序进行排序

理想输出如下所示:

    City      Score
[1] DC        -1
[2] Yokohama  -1
[3] Nashville -3
[4] Tokyo     -4
[5] Newyork   -6
[6] Boston    -8
[7] Chicago   -9

这将返回作为行名的城市名称,但如果您希望它成为列,这是一个简单的复制

Refcities <- list(Boston=Boston, Chicago=Chicago, DC=DC, Nashville=Nashville, Newyork=Newyork, Tokyo=Tokyo, Yokohama=Yokohama)

score <- sort(sapply(Refcities, unweighted, Elendel), decreasing = TRUE)

df1 <- data.frame(score)

> df1
          score
DC           -1
Yokohama     -1
Nashville    -3
Tokyo        -4
Newyork      -6
Boston       -8
Chicago      -9

Refcities这将以行名的形式返回城市名称,但如果您希望将其作为列,这是一个简单的复制

Refcities <- list(Boston=Boston, Chicago=Chicago, DC=DC, Nashville=Nashville, Newyork=Newyork, Tokyo=Tokyo, Yokohama=Yokohama)

score <- sort(sapply(Refcities, unweighted, Elendel), decreasing = TRUE)

df1 <- data.frame(score)

> df1
          score
DC           -1
Yokohama     -1
Nashville    -3
Tokyo        -4
Newyork      -6
Boston       -8
Chicago      -9

Refcities保留您的
outersect
功能,然后前往
tidyverse

#outersect
outersect <- function(x, y) {
big.vec <- c(x, y)
duplicates <- big.vec[duplicated(big.vec)]
setdiff(big.vec, unique(duplicates))
}

#Reference set
Newyork <- c("ant", "bat", "cat", "dog", "unicorn", "camel", "snake", "monkey", "donkey")
Tokyo <-    c("unicorn")
Yokohama <- c("ant", "bat", "cat", "dog")
Chicago <- c("bird", "ant", "bat", "cat", "bear", "dog", "snake", "monkey", "mouse", " donkey", "octopus", "camel")
Nashville <-c("ant", "bat", "octopus") 
DC <-   c("ant", "dog", "cat", "bird")
Boston <-   c("ant", "bird", "cat", "bear", "camel", "snake", "mouse", "octopus")

#query set
Elendel <- c("wolf", "dog" ,"ant")

library(tidyverse)
#combining References
cities <- c('Boston', 'Chicago', 'DC', 'Nashville', 'Newyork', 'Tokyo', 'Yokohama')
Refcities <- list(Boston = Boston
                  , Chicago = Chicago
                  , DC = DC
                  , Nashville = Nashville
                  , Newyork = Newyork
                  , Tokyo = Tokyo
                  , Yokohama = Yokohama)

df <- data_frame(City = cities
                     , inter = sapply(Refcities, function(x) {
                         length(intersect(x, Elendel))
                         })
                     , outer = sapply(Refcities, function(x){
                         length(outersect(x, Elendel))
                         })
                     ) %>%
  mutate(Score = inter - outer) %>%
  arrange(desc(Score)) %>% select(City, Score)
#outersect

outersect保留您的
outersect
功能,然后前往
tidyverse

#outersect
outersect <- function(x, y) {
big.vec <- c(x, y)
duplicates <- big.vec[duplicated(big.vec)]
setdiff(big.vec, unique(duplicates))
}

#Reference set
Newyork <- c("ant", "bat", "cat", "dog", "unicorn", "camel", "snake", "monkey", "donkey")
Tokyo <-    c("unicorn")
Yokohama <- c("ant", "bat", "cat", "dog")
Chicago <- c("bird", "ant", "bat", "cat", "bear", "dog", "snake", "monkey", "mouse", " donkey", "octopus", "camel")
Nashville <-c("ant", "bat", "octopus") 
DC <-   c("ant", "dog", "cat", "bird")
Boston <-   c("ant", "bird", "cat", "bear", "camel", "snake", "mouse", "octopus")

#query set
Elendel <- c("wolf", "dog" ,"ant")

library(tidyverse)
#combining References
cities <- c('Boston', 'Chicago', 'DC', 'Nashville', 'Newyork', 'Tokyo', 'Yokohama')
Refcities <- list(Boston = Boston
                  , Chicago = Chicago
                  , DC = DC
                  , Nashville = Nashville
                  , Newyork = Newyork
                  , Tokyo = Tokyo
                  , Yokohama = Yokohama)

df <- data_frame(City = cities
                     , inter = sapply(Refcities, function(x) {
                         length(intersect(x, Elendel))
                         })
                     , outer = sapply(Refcities, function(x){
                         length(outersect(x, Elendel))
                         })
                     ) %>%
  mutate(Score = inter - outer) %>%
  arrange(desc(Score)) %>% select(City, Score)
#outersect

outerSelect为什么不使用矩阵或数据框,并使用向量化调用对动物进行计数:
table
tapply
ave
aggregate
?你能解释一下结果吗。例如,当DC与至少一个其他城市共享其所有动物时,DC=-1是如何实现的?为什么不使用矩阵或数据帧并使用向量化调用对动物进行计数:
tapply
ave
聚合
?你能解释一下结果吗。例如,当DC=-1与至少一个其他城市共享其所有动物时,它是如何做到的?