我有下面提到的excel表。
ID column1 column2 column3
1 Apple apple - x Apple - le
2 Mango banana cat
3 Dog DOG - A DOG - B
我想匹配column1,column2和column3的字符串(这三列中的一个单词相似)。
必需输出:
ID column1 column2 column3 Status
1 Apple apple - x Apple - le True
2 Mango banana cat False
3 Dog DOG - A DOG - B True
答案 0 :(得分:2)
代码 LO BO 的想法。
在Excel中使用 VBA ,尝试以下用户定义函数:
Public Function ThreeWay(s1 As String, s2 As String, s3 As String) As Boolean
Dim a As String, b As String, c As String
a = Split(s1, " ")(0)
b = Split(s2, " ")(0)
c = Split(s3, " ")(0)
ThreeWay = False
If a = b And a = c Then ThreeWay = True
End Function
修改#1:强>
只有两列,请使用:
Public Function TwoWay(s1 As String, s2 As String) As Boolean
Dim a As String, b As String, c As String
a = Split(s1, " ")(0)
b = Split(s2, " ")(0)
TwoWay = False
If a = b Then TwoWay = True
End Function
修改#2:强>
要忽略大小写差异,请尝试:
Public Function ThreeWayIC(s1 As String, s2 As String, s3 As String) As Boolean
Dim a As String, b As String, c As String
a = Split(UCase(s1), " ")(0)
b = Split(UCase(s2), " ")(0)
c = Split(UCase(s3), " ")(0)
ThreeWayIC = False
If a = b And a = c Then ThreeWayIC = True
End Function
答案 1 :(得分:1)
基地R中有这样的事情吗?
lst1 <- lapply(strsplit(as.character(df$column2), " - "), function(x)
trimws(tolower(x)));
lst2 <- lapply(strsplit(as.character(df$column3), " - "), function(x)
trimws(tolower(x)));
df$Status <- sapply(1:nrow(df), function(i)
any(lst1[[i]] %in% lst2[[i]]));
df;
# ID column1 column2 column3 Status
#1 1 Apple apple - x Apple - le TRUE
#2 2 Mango banana cat FALSE
#3 3 Dog DOG - A DOG - B TRUE
说明:根据column2
拆分column3
和" - "
中的条目,修剪任何空格并将所有条件转换为小写;然后检查any
和column2
中是否存在column3
个字词。
df <- read.table(text =
"ID column1 column2 column3
1 Apple 'apple - x' 'Apple - le'
2 Mango 'banana' cat
3 Dog 'DOG - A' 'DOG - B'", header = T);
答案 2 :(得分:0)
如果使用Split function获取单元格的第一个单词(请参阅“从文本字符串的开头返回文本,直到第一个空格”),它应该有效,然后使用匹配数来计算COUNTIF function。