提取与 R 中列范围之外的字符串匹配的值

问题描述 投票:0回答:1

在 R 中编写相关代码。我有一个数据框(称为 realData),其中有 10 个名为“RealAttribute_X”的变量,其中 X 是 1 到 10(含)之间的数字。 每栏包含十个属性之一:“智力”、“吸引力”、“魅力”、“野心”、“懒惰”、“慷慨”、“快乐”、“友善”、“傲慢”、“冷静”。属性随机分配给 10 个“RealAttribute_X”中的 1 个。

dt<-structure(list(session_id = c("17472631", "17472632", "17472633", 
"17472635", "17472636", "17472638"), RealAttribute_1 = c("Moderately ugly", 
"Very dull", "Very distant", "Very joyful", "Moderately joyful", 
"Very distant"), RealAttribute_2 = c("Very nervous", "Very gloomy", 
"Very generous", "Moderately charismatic", "Moderately hard working", 
"Moderately modest"), RealAttribute_3 = c("Slightly generous", 
"Moderately ugly", "Moderately arrogant", "Moderately calm", 
"Moderately charismatic", "Moderately charismatic"), RealAttribute_4 = c("Moderately arrogant", 
"Slightly generous", "Very dull", "Moderately distant", "Slightly distant", 
"Slightly ambitious"), RealAttribute_5 = c("Slightly unambitious", 
"Moderately calm", "Moderately unambitious", "Moderately lazy", 
"Very modest", "Moderately intelligent"), RealAttribute_6 = c("Slightly dull", 
"Slightly ambitious", "Very calm", "Moderately ambitious", "Moderately generous", 
"Moderately generous"), RealAttribute_7 = c("Very intelligent", 
"Slightly distant", "Very intelligent", "Slightly ugly", "Very good-looking", 
"Very lazy"), RealAttribute_8 = c("Very joyful", "Slightly modest", 
"Slightly joyful", "Very arrogant", "Very ambitious", "Slightly good-looking"
), RealAttribute_9 = c("Very distant", "Very lazy", "Slightly good-looking", 
"Very generous", "Moderately intelligent", "Moderately gloomy"
), RealAttribute_10 = c("Slightly lazy", "Moderately intelligent", 
"Slightly hard working", "Moderately intelligent", "Very calm", 
"Moderately nervous")), class = "data.frame", row.names = c(NA, 
-6L))

head(dt)

每个属性可以有六个值之一。属性及其可选值是:智力=“非常不聪明”、“中等不聪明”、“稍微不聪明”、“稍微聪明”、“中等聪明”、“非常聪明” 吸引力=“非常丑”,“中等丑”,“稍微丑”,“稍微好看”,“中等好看”,“非常好看” 懒惰=“非常懒”、“中等懒”、“稍微懒”、“稍微努力”、“中等努力”、“非常努力” 友善 =“非常疏远”、“中等疏远”、“稍微疏远”、“稍微友好”、“一般友好”、“非常友好” charisma =“非常迟钝”、“中等迟钝”、“稍微迟钝”、“稍微有魅力”、“中等魅力”、“非常有魅力” 平静 =“非常紧张”、“中等紧张”、“有点紧张”、“稍微平静”、“中等平静”、“非常平静” 慷慨=“非常小气”,“中等小气”,“稍微小气”,“稍微慷慨”,“中等慷慨”,“非常慷慨” Joyfullness =“非常阴郁”、“中等阴郁”、“稍微阴郁”、“稍微快乐”、“中等快乐”、“非常快乐” arrogance =“非常傲慢”、“适度傲慢”、“稍微傲慢”、“稍微谦虚”、“适度谦虚”、“非常谦虚” 雄心壮志=“非常没有雄心”,“一般没有雄心”,“稍微没有雄心”,“稍微有雄心”,“中等雄心”,“非常雄心勃勃”

我正在尝试用 R 编写一段代码,创建 10 个新列,每个属性对应一个(“智力”、“吸引力”、“魅力”、“野心”、“懒惰”、“慷慨”、“快乐”、“友善”、“傲慢”、“冷静”),在现有数据框中找到包含相关属性的正确变量,并将该属性的值分配给正确的新变量。

例如,这是数据帧的前四行: RealAttribute_1:“丑陋”、“很沉闷”、“很遥远”、“很快乐”

因此前四行的最终结果应如下所示: 智力:“非常聪明”、“中等智力”、“非常聪明”、“中等智力”

我定义了所有属性值

attribute_values <- list(
  intelligence = c("Very unintelligent", "Moderately unintelligent", "Slightly unintelligent", 
                   "Slightly intelligent", "Moderately intelligent", "Very intelligent"),
  attractiveness = c("Very ugly", "Moderately ugly", "Slightly ugly", 
                     "Slightly good-looking", "Moderately good-looking", "Very good-looking"),
  laziness = c("Very lazy", "Moderately lazy", "Slightly lazy", 
               "Slightly hard working", "Moderately hard working", "Very hard working"),
  friendliness = c("Very distant", "Moderately distant", "Slightly distant", 
                   "Slightly friendly", "Moderately friendly", "Very friendly"),
  charisma = c("Very dull", "Moderately dull", "Slightly dull", 
               "Slightly charismatic", "Moderately charismatic", "Very charismatic"),
  calmness = c("Very nervous", "Moderately nervous", "Slightly nervous", 
               "Slightly calm", "Moderately calm", "Very calm"),
  generosity = c("Very stingy", "Moderately stingy", "Slightly stingy", 
                 "Slightly generous", "Moderately generous", "Very generous"),
  joyfullness = c("Very gloomy", "Moderately gloomy", "Slightly gloomy", 
                  "Slightly joyful", "Moderately joyful", "Very joyful"),
  arrogance = c("Very arrogant", "Moderately arrogant", "Slightly arrogant", 
                "Slightly modest", "Moderately modest", "Very modest"),
  ambition = c("Very unambitious", "Moderately unambitious", "Slightly unambitious", 
               "Slightly ambitious", "Moderately ambitious", "Very ambitious")
)

但开始检查我的代码中的第一个变量(智能):

intelligence_values <- c("Very unintelligent", "Moderately unintelligent", "Slightly unintelligent", 
                         "Slightly intelligent", "Moderately intelligent", "Very intelligent")

realData$Intelligence <- apply(dt[, grep("RealAttribute_", colnames(realData), value = TRUE)], 1, function(row) {
  match_value <- which(row %in% intelligence_values)[1]
  if (is.na(match_value)) {
    return(NA)
  } else {
    return(intelligence_values[match_value])
  }
})

此代码仅将 NA 返回到“情报”列。 我也尝试过:

realData <- realData %>%
  rowwise() %>%
  mutate(Intelligence = intelligence_values[match(c_across(starts_with("RealAttribute")), intelligence_values)])

但出现以下错误:

c_across()
中的错误: !只能在数据屏蔽动词内使用,例如
mutate()
filter()
group_by()

运行 rlang::last_trace() 后:

c_across()
中的错误: !只能在数据屏蔽动词内使用,例如
mutate()
filter()
group_by()
。 **--- 回溯: x

  1. +-realData %>% rowwise() %>% ...
  2. -plyr::变异(...)
  3. -base::eval(cols[[col]],.data,parent.frame())
  4. \-base::eval(cols[[col]], .data, parent.frame())
    
  5.   +-base::match(c_across(starts_with("RealAttribute")), intelligence_values)
    
  6.   \-dplyr::c_across(starts_with("RealAttribute"))
    

运行 rlang::last_trace(drop = FALSE) 以查看 4 个隐藏帧。

有什么想法出了什么问题或者我应该怎么写吗? 预先感谢!

r search
1个回答
0
投票

如果答案的形式始终是副词属性,那么听起来您不需要引用另一个表格。您可以像这样重塑数据:

library(tidyverse)
dt |>
  pivot_longer(-session_id) |>
  separate(value, c("degree", "attribute"), sep = " ", extra = "merge") %>%
  select(-name) %>%
  pivot_wider(names_from = attribute, values_from = degree)

结果

# A tibble: 6 × 18
  session_id ugly    nervous generous arrogant unambitious dull  intelligent joyful distant lazy  gloomy calm  ambitious modest `good-looking` `hard working` charismatic
  <chr>      <chr>   <chr>   <chr>    <chr>    <chr>       <chr> <chr>       <chr>  <chr>   <chr> <chr>  <chr> <chr>     <chr>  <chr>          <chr>          <chr>      
1 17472631   Modera… Very    Slightly Moderat… Slightly    Slig… Very        Very   Very    Slig… NA     NA    NA        NA     NA             NA             NA         
2 17472632   Modera… NA      Slightly NA       NA          Very  Moderately  NA     Slight… Very  Very   Mode… Slightly  Sligh… NA             NA             NA         
3 17472633   NA      NA      Very     Moderat… Moderately  Very  Very        Sligh… Very    NA    NA     Very  NA        NA     Slightly       Slightly       NA         
4 17472635   Slight… NA      Very     Very     NA          NA    Moderately  Very   Modera… Mode… NA     Mode… Moderate… NA     NA             NA             Moderately 
5 17472636   NA      NA      Moderat… NA       NA          NA    Moderately  Moder… Slight… NA    NA     Very  Very      Very   Very           Moderately     Moderately 
6 17472638   NA      Modera… Moderat… NA       NA          NA    Moderately  NA     Very    Very  Moder… NA    Slightly  Moder… Slightly       NA             Moderately 
© www.soinside.com 2019 - 2024. All rights reserved.