gpt4 book ai didi

r - 如何解码包含 R 中另一列文本的列?

转载 作者:行者123 更新时间:2023-12-05 09:28:17 25 4
gpt4 key购买 nike

我有一个数据框,在答案列中包含编码的调查答案,键作为字符列中的一个字符串:

df <- data.frame(answer = c(1, 2, 1, 3, 1),
key = c("1 = Answer One 2 = Answer Two 3 = Answer Three", "1 = Answer ABC 2 = Answer DEF 3 = Answer GHI",
"1 = Answer abc 2 = Answer def 3 = Answer ghi", "1 = Answer One 2 = Answer Two 3 = Answer Three",
"1 = Answer ABC 2 = Answer DEF 3 = Answer GHI"))

print(df)

answer key
1 1 "1 = Answer One 2 = Answer Two 3 = Answer Three"
2 2 "1 = Answer ABC 2 = Answer DEF 3 = Answer GHI"
3 1 "1 = Answer abc 2 = Answer def 3 = Answer ghi"
4 3 "1 = Answer One 2 = Answer Two 3 = Answer Three"
5 1 "1 = Answer ABC 2 = Answer DEF 3 = Answer GHI"

如何使用键列中的数据对答案列进行解码,以便获得此结果?

df_result <- data.frame(answer = c(1, 2, 1, 3, 1),
key = c("1 = Answer One 2 = Answer Two 3 = Answer Three", "1 = Answer ABC 2 = Answer DEF 3 = Answer GHI",
"1 = Answer abc 2 = Answer def 3 = Answer ghi", "1 = Answer One 2 = Answer Two 3 = Answer Three",
"1 = Answer ABC 2 = Answer DEF 3 = Answer GHI"),
answer_decoded = c("Answer One", "Answer DEF", "Answer abc", "Answer Three","Answer ABC"))

print(df_result)

answer key answer_decoded
1 1 "1 = Answer One 2 = Answer Two 3 = Answer Three" "Answer One"
2 2 "1 = Answer ABC 2 = Answer DEF 3 = Answer GHI" "Answer DEF"
3 1 "1 = Answer abc 2 = Answer def 3 = Answer ghi" "Answer abc"
4 3 "1 = Answer One 2 = Answer Two 3 = Answer Three" "Answer Three"
5 1 "1 = Answer ABC 2 = Answer DEF 3 = Answer GHI" "Answer ABC"

我无法使用因子标签,因为我有太多不同的项目无法手动创建它们。

最佳答案

我们可以根据“answer”值提取子字符串 - 使用 str_c 创建要提取的模式,即粘贴带有空格的“answer”,后跟 =和一个或多个非数字字符 (\\D+) 并删除前缀部分,包括 = 和带有 trimws

library(stringr)
library(dplyr)
df %>%
mutate(answer_decoded = trimws(str_extract(key,
str_c(answer, ' = \\D+')), whitespace = ".*=\\s+|\\s+"))

-输出

  answer                                            key answer_decoded
1 1 1 = Answer One 2 = Answer Two 3 = Answer Three Answer One
2 2 1 = Answer ABC 2 = Answer DEF 3 = Answer GHI Answer DEF
3 1 1 = Answer abc 2 = Answer def 3 = Answer ghi Answer abc
4 3 1 = Answer One 2 = Answer Two 3 = Answer Three Answer Three
5 1 1 = Answer ABC 2 = Answer DEF 3 = Answer GHI Answer ABC

关于r - 如何解码包含 R 中另一列文本的列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71489048/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com