r - 获取R数据框中包含特定字符的每行的列号

标签 r dataframe

如果我有如下所示的数据框。

a <- c('A', 'b', 'c')
b <- c('b', 'c', 'A')
c <- c('c', 'A', 'b')
df <- data.frame(a, b, c)

df
  a b c
1 A b c
2 b c A
3 c A b

我想生成如下所示的附加列。基本上,df$b_pos 指定“b”位于“A”之前还是之后(同样的原则也适用于 df$c_pos)。

df$b_pos <- c('after A', 'before A', 'after A')
df$c_pos <- c('after A', 'before A', 'before A')

df
  a b c    b_pos    c_pos
1 A b c  after A  after A
2 b c A before A before A
3 c A b  after A before A

我想编写如下所示的行,以便我可以自动化该过程。

df$b_pos <- ifelse(get_the_column_index_of_A > 
                     get_the_column_index_of_b, 'before A', 'after A')
df$c_pos <- ifelse(get_the_column_index_of_A > 
                     get_the_column_index_of_c, 'before A', 'after A')

如果有人能给我一些建议,用什么来代替“get_the_column_index_of_A”,我将非常感激。

最佳答案

我们可以使用max.col来做到这一点

df[c('b_pos', 'c_pos')] <- lapply(letters[2:3], function(x) 
         c("before A", "after A")[1+(max.col(df=="A", "first") < max.col(df==x, "first"))])
df
#  a b c    b_pos    c_pos
#1 A b c  after A  after A
#2 b c A before A before A
#3 c A b  after A before A

或者另一种选择是按行粘贴数据集并使用grepl检查模式

df[c('b_pos', 'c_pos')] <- lapply(c("A.*b",  "A.*c"), function(x) 
           c("before A", "after A")[grepl(x, do.call(paste0, df))+1L])

关于r - 获取R数据框中包含特定字符的每行的列号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37477209/

相关文章:

r - 将缺少日期的数据框转换为时间序列

r - 基于另一个数据帧创建一个新的数据帧(使用循环或其他方式)

r - 无法使用 scales 包格式化 xaxis

python - 如何将值映射到位?

python - 如何从 csv 中读取 pandas 中的 NaN(氮化钠)作为字符串而不是 NaN(不是数字)?

r - 比熔化和 rbind 更快的替代方案

R - 子集类别

function - Pareto 函数与 Julia 中的数据帧?

python - 是否可以对数据帧中的列列表/子集运行替换?

r - 在数据框上定义和应用自定义容器