R 在数据框的新列中打印匹配位置
我要做的是在一组列上搜索,然后返回一个包含 1.列中的字符串及其来源的列的名称R 在数据框的新列中打印匹配位置,r,dataframe,match,R,Dataframe,Match,我要做的是在一组列上搜索,然后返回一个包含 1.列中的字符串及其来源的列的名称 df <- structure(list(ID = c("A1.1234567_10", "A1.1234567_20"), var1 = c("NORMAL", "NORMAL"), var2 = c("NORMAL", "SUSPECTED"), var3 = c("NORMAL", "NORMAL"
df <- structure(list(ID = c("A1.1234567_10", "A1.1234567_20"),
var1 = c("NORMAL", "NORMAL"),
var2 = c("NORMAL", "SUSPECTED"),
var3 = c("NORMAL", "NORMAL"),
var4 = c("NORMAL", "NORMAL"),
var5 = c("NORMAL", "NORMAL"),
var6 = c("NORMAL", "NORMAL"),
var7 = c("NORMAL", "ABNORMAL"),
var8 = c("NORMAL", "NORMAL")),
.Names = c("ID", "var1", "var2", "var3", "var4", "var5", "var6", "var7", "var8"),
class = "data.frame", row.names = c(NA, -2L))
ID var1 var2 var3 var4 var5 var6 var7 var8
A1.1234567_10 NORMAL NORMAL NORMAL NORMAL NORMAL NORMAL NORMAL NORMAL
A1.1234567_20 NORMAL SUSPECTED NORMAL NORMAL NORMAL NORMAL ABNORMAL NORMAL
我知道我可以自己为它们编制索引,但最终查看它们的人需要在每一行中将其可视化。应用
函数将成为循环遍历每一行的主力。在每一行上使用自定义函数编译“坏”值的字符串向量及其对应的列名索引。在连接行上找到的所有坏值列组合之前,请将坏值和列名组合在一起。最后,该行应作为可添加到原始数据帧的向量返回
#This example uses the dataframe, `df` you defined in your question.
#
# this function works on one row at a time and can accept one or more
# accepted values as a character vector.
library(magrittr)
build_nonnorm_str <- function(row, col_names, norm_value) {
# get the index of any bad values, excluding the column named "ID"
bad_col_indx <- which(!row %in% norm_value &
!names(row) == "ID")
# appropriately assign NA to rows with no bad values,
# otherwise put together the string to be appended to
# the new dataframe
if (length(bad_col_indx > 0)) {
abnorm_str <- paste0(row[bad_col_indx],
"_",
col_names[bad_col_indx],
sep = " ") %>%
paste0(collapse = "") %>%
trimws()
} else {
abnorm_str <- NA
}
return(abnorm_str)
}
# Use the apply function to send the function one rows worth of data
# and append it to the new column
df$abnormal_summary <- apply(df,
1,
build_nonnorm_str,
col_names = names(df),
norm_value = "NORMAL")
#此示例使用您在问题中定义的数据帧'df'。
#
#此函数一次只能处理一行,可以接受一行或多行
#接受值作为字符向量。
图书馆(magrittr)
构建\u非标准\u str%
trimws()
}否则{
异常你是想返回所有非“正常”的列,还是只返回那些“可疑”或“异常”的列?这非常有效。我想我也可以通过更改bad\u col\u index
和更改一些基于bad\u col\u indx@Randy的参数来选择这些列,谢谢你的关注g函数调用中的输入错误
#This example uses the dataframe, `df` you defined in your question.
#
# this function works on one row at a time and can accept one or more
# accepted values as a character vector.
library(magrittr)
build_nonnorm_str <- function(row, col_names, norm_value) {
# get the index of any bad values, excluding the column named "ID"
bad_col_indx <- which(!row %in% norm_value &
!names(row) == "ID")
# appropriately assign NA to rows with no bad values,
# otherwise put together the string to be appended to
# the new dataframe
if (length(bad_col_indx > 0)) {
abnorm_str <- paste0(row[bad_col_indx],
"_",
col_names[bad_col_indx],
sep = " ") %>%
paste0(collapse = "") %>%
trimws()
} else {
abnorm_str <- NA
}
return(abnorm_str)
}
# Use the apply function to send the function one rows worth of data
# and append it to the new column
df$abnormal_summary <- apply(df,
1,
build_nonnorm_str,
col_names = names(df),
norm_value = "NORMAL")