创建一个列,该列根据R中字符串的顺序添加一个数值

lf5gs5x2  于 2023-01-28  发布在  其他
关注(0)|答案(1)|浏览(118)

我在征求问题排名时得到了回复,我对每个人的回复如下所示:

structure(list(Rank = "Shifting angler preferences and behaviors;Increasing effort and fishing power;R3 (Recruitment, Retention, and Reactivation);Economic impacts;Climate change;"), row.names = c(NA, 
-1L), class = "data.frame")

我尝试创建一个列,根据每个字符串的顺序给出每个Rank的数值。我希望每个字符串有五个列,用";"分隔,如下所示:

Shifting angler preferences and behaviors 
5

Increasing effort and fishing power
4

R3 (Recruitment, Retention, and Reactivation)
3

Economic impacts
2

Climate change
1

这只是一个单一的回应,我们征求了问题的排名,我试图形象化的React。
谢谢!

vecaoik1

vecaoik11#

猜一猜,我认为您想知道每个以;分隔的子字符串在向量中出现的顺序,其中第一个是“最高优先级”(因此是5),等等。

fun <- function(x, table = NA) {
  if (anyNA(table)) {
    # obtained in the question, assuming that these 
    table <- trimws(strsplit("Shifting angler preferences and behaviors;Increasing effort and fishing power;R3 (Recruitment, Retention, and Reactivation);Economic impacts;Climate change", ";")[[1]])
  }
  bind_rows(
    lapply(strsplit(x, ";"),
           function(z) setNames(length(table) + 1L - match(table, trimws(z)), table))
  )
}

使用增强数据进行演示:

quux <- structure(list(Rank = c("Shifting angler preferences and behaviors;Increasing effort and fishing power;R3 (Recruitment, Retention, and Reactivation);Economic impacts;Climate change;", "Economic impacts;Climate change;Shifting angler preferences and behaviors;Increasing effort and fishing power;R3 (Recruitment, Retention, and Reactivation);")), class = "data.frame", row.names = c(NA, -2L))

out <- quux %>%
  mutate(fun(Rank))
str(out)
# 'data.frame': 2 obs. of  6 variables:
#  $ Rank                                         : chr  "Shifting angler preferences and behaviors;Increasing effort and fishing power;R3 (Recruitment, Retention, and R"| __truncated__ "Economic impacts;Climate change;Shifting angler preferences and behaviors;Increasing effort and fishing power;R"| __truncated__
#  $ Shifting angler preferences and behaviors    : int  5 3
#  $ Increasing effort and fishing power          : int  4 2
#  $ R3 (Recruitment, Retention, and Reactivation): int  3 1
#  $ Economic impacts                             : int  2 5
#  $ Climate change                               : int  1 4
out
#                                                                                                                                                           Rank Shifting angler preferences and behaviors Increasing effort and fishing power R3 (Recruitment, Retention, and Reactivation) Economic impacts Climate change
# 1 Shifting angler preferences and behaviors;Increasing effort and fishing power;R3 (Recruitment, Retention, and Reactivation);Economic impacts;Climate change;                                         5                                   4                                             3                2              1
# 2 Economic impacts;Climate change;Shifting angler preferences and behaviors;Increasing effort and fishing power;R3 (Recruitment, Retention, and Reactivation);                                         3                                   2                                             1                5              4

相关问题