Identifying if one field is partial derived from another in R





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







-1















So given a pattern, say two letters, and the position of the pattern, is it a prefix, suffix or in the middle, I need to identify if a field is partial derived from another. So for example given the following dataset



 data.V1 data.V2
1 GH GH1001
2 FD FD2002
3 TH 2345TH
4 ED ED56763
5 US 4345US
6 FG F6736tG


if LL is the pattern for column one where LL refers to two letters in this case. If the pattern for column 2 is LL#. This indicates that the position of the pattern is the first elements of each element in row 2. So in the dataset above rows 1,2&4 would obey the pattern.



I have tried if then statements but these did not work if the pattern was in the middle , #LL#. I have also tried the function regmathes but that did not work either.










share|improve this question























  • Useful link for trying out and better understanding regular expressions

    – Nutle
    Nov 23 '18 at 14:15











  • For example, in the link above, try [A-Z]{2}, ^[A-Z]{2} and [A-Z]{2}$ for various entries.

    – Nutle
    Nov 23 '18 at 14:21













  • @Nutle thanks. Is it possible in regular expressions to specify where to start? For example if I wanted to check were the 3rd and 4th elements of an entry both letters.

    – Sarah
    Nov 23 '18 at 15:08











  • ^ for beginning, $ for the end, . as a filler. So for the exactly third and 4th, try ..[A-Z]{2}, note the two dots. Finally, questions with regex are very easily googleable, since they're a common standard everywhere

    – Nutle
    Nov 23 '18 at 15:13


















-1















So given a pattern, say two letters, and the position of the pattern, is it a prefix, suffix or in the middle, I need to identify if a field is partial derived from another. So for example given the following dataset



 data.V1 data.V2
1 GH GH1001
2 FD FD2002
3 TH 2345TH
4 ED ED56763
5 US 4345US
6 FG F6736tG


if LL is the pattern for column one where LL refers to two letters in this case. If the pattern for column 2 is LL#. This indicates that the position of the pattern is the first elements of each element in row 2. So in the dataset above rows 1,2&4 would obey the pattern.



I have tried if then statements but these did not work if the pattern was in the middle , #LL#. I have also tried the function regmathes but that did not work either.










share|improve this question























  • Useful link for trying out and better understanding regular expressions

    – Nutle
    Nov 23 '18 at 14:15











  • For example, in the link above, try [A-Z]{2}, ^[A-Z]{2} and [A-Z]{2}$ for various entries.

    – Nutle
    Nov 23 '18 at 14:21













  • @Nutle thanks. Is it possible in regular expressions to specify where to start? For example if I wanted to check were the 3rd and 4th elements of an entry both letters.

    – Sarah
    Nov 23 '18 at 15:08











  • ^ for beginning, $ for the end, . as a filler. So for the exactly third and 4th, try ..[A-Z]{2}, note the two dots. Finally, questions with regex are very easily googleable, since they're a common standard everywhere

    – Nutle
    Nov 23 '18 at 15:13














-1












-1








-1








So given a pattern, say two letters, and the position of the pattern, is it a prefix, suffix or in the middle, I need to identify if a field is partial derived from another. So for example given the following dataset



 data.V1 data.V2
1 GH GH1001
2 FD FD2002
3 TH 2345TH
4 ED ED56763
5 US 4345US
6 FG F6736tG


if LL is the pattern for column one where LL refers to two letters in this case. If the pattern for column 2 is LL#. This indicates that the position of the pattern is the first elements of each element in row 2. So in the dataset above rows 1,2&4 would obey the pattern.



I have tried if then statements but these did not work if the pattern was in the middle , #LL#. I have also tried the function regmathes but that did not work either.










share|improve this question














So given a pattern, say two letters, and the position of the pattern, is it a prefix, suffix or in the middle, I need to identify if a field is partial derived from another. So for example given the following dataset



 data.V1 data.V2
1 GH GH1001
2 FD FD2002
3 TH 2345TH
4 ED ED56763
5 US 4345US
6 FG F6736tG


if LL is the pattern for column one where LL refers to two letters in this case. If the pattern for column 2 is LL#. This indicates that the position of the pattern is the first elements of each element in row 2. So in the dataset above rows 1,2&4 would obey the pattern.



I have tried if then statements but these did not work if the pattern was in the middle , #LL#. I have also tried the function regmathes but that did not work either.







r






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 23 '18 at 14:11









Sarah Sarah

65




65













  • Useful link for trying out and better understanding regular expressions

    – Nutle
    Nov 23 '18 at 14:15











  • For example, in the link above, try [A-Z]{2}, ^[A-Z]{2} and [A-Z]{2}$ for various entries.

    – Nutle
    Nov 23 '18 at 14:21













  • @Nutle thanks. Is it possible in regular expressions to specify where to start? For example if I wanted to check were the 3rd and 4th elements of an entry both letters.

    – Sarah
    Nov 23 '18 at 15:08











  • ^ for beginning, $ for the end, . as a filler. So for the exactly third and 4th, try ..[A-Z]{2}, note the two dots. Finally, questions with regex are very easily googleable, since they're a common standard everywhere

    – Nutle
    Nov 23 '18 at 15:13



















  • Useful link for trying out and better understanding regular expressions

    – Nutle
    Nov 23 '18 at 14:15











  • For example, in the link above, try [A-Z]{2}, ^[A-Z]{2} and [A-Z]{2}$ for various entries.

    – Nutle
    Nov 23 '18 at 14:21













  • @Nutle thanks. Is it possible in regular expressions to specify where to start? For example if I wanted to check were the 3rd and 4th elements of an entry both letters.

    – Sarah
    Nov 23 '18 at 15:08











  • ^ for beginning, $ for the end, . as a filler. So for the exactly third and 4th, try ..[A-Z]{2}, note the two dots. Finally, questions with regex are very easily googleable, since they're a common standard everywhere

    – Nutle
    Nov 23 '18 at 15:13

















Useful link for trying out and better understanding regular expressions

– Nutle
Nov 23 '18 at 14:15





Useful link for trying out and better understanding regular expressions

– Nutle
Nov 23 '18 at 14:15













For example, in the link above, try [A-Z]{2}, ^[A-Z]{2} and [A-Z]{2}$ for various entries.

– Nutle
Nov 23 '18 at 14:21







For example, in the link above, try [A-Z]{2}, ^[A-Z]{2} and [A-Z]{2}$ for various entries.

– Nutle
Nov 23 '18 at 14:21















@Nutle thanks. Is it possible in regular expressions to specify where to start? For example if I wanted to check were the 3rd and 4th elements of an entry both letters.

– Sarah
Nov 23 '18 at 15:08





@Nutle thanks. Is it possible in regular expressions to specify where to start? For example if I wanted to check were the 3rd and 4th elements of an entry both letters.

– Sarah
Nov 23 '18 at 15:08













^ for beginning, $ for the end, . as a filler. So for the exactly third and 4th, try ..[A-Z]{2}, note the two dots. Finally, questions with regex are very easily googleable, since they're a common standard everywhere

– Nutle
Nov 23 '18 at 15:13





^ for beginning, $ for the end, . as a filler. So for the exactly third and 4th, try ..[A-Z]{2}, note the two dots. Finally, questions with regex are very easily googleable, since they're a common standard everywhere

– Nutle
Nov 23 '18 at 15:13












1 Answer
1






active

oldest

votes


















0














apply(df,1,function(x) grepl(paste0("^",x["data.V1"]),x["data.V2"]))


For every row (that's the 1 in apply), this will check whether the contents of data.V1 appears right at the beginning of data.V2 (^ means the beginning of a string for regular expressions).



Result:



    1     2     3     4     5     6 
TRUE TRUE FALSE TRUE FALSE FALSE


Replace the grepl first argument with the following for:



End of string: paste0(x["data.V1"],"$")
Middle of string: paste0(".+",x["data.V1"],".+")
After n characters (n defined elsewhere): paste0(".{",n,"}",x["data.V1"])


(for the last one, the form "{n}" means the last character is repeated n times. Since it is preceded by ".", it means any n characters.)






share|improve this answer


























  • Thanks.That worked great if the pattern was at the beginning. Would it be possible to do something similar if the pattern was in the middle or end of a entry? For example row 5 in the dataset above.

    – Sarah
    Nov 23 '18 at 14:39











  • see the edited answer above: just replace the first argument of the grepl with either option.

    – iod
    Nov 23 '18 at 16:12











  • @Sarah don't forget to upvote and accept!

    – iod
    Nov 23 '18 at 16:29











  • @ iod. Just wondering is it possible to generalize the statement. For example I tried apply(data,1,function(x) grepl(paste0('rep(.,n)+',x["V1"],'...+'),x["V3"])). So that I can specify the position outside the function.

    – Sarah
    Nov 23 '18 at 17:09













  • Not sure what you're trying to achieve with that rep() call. As noted in the comments to your question - it's a good idea to familiarize yourself with regex syntax.

    – iod
    Nov 23 '18 at 17:19












Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448236%2fidentifying-if-one-field-is-partial-derived-from-another-in-r%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














apply(df,1,function(x) grepl(paste0("^",x["data.V1"]),x["data.V2"]))


For every row (that's the 1 in apply), this will check whether the contents of data.V1 appears right at the beginning of data.V2 (^ means the beginning of a string for regular expressions).



Result:



    1     2     3     4     5     6 
TRUE TRUE FALSE TRUE FALSE FALSE


Replace the grepl first argument with the following for:



End of string: paste0(x["data.V1"],"$")
Middle of string: paste0(".+",x["data.V1"],".+")
After n characters (n defined elsewhere): paste0(".{",n,"}",x["data.V1"])


(for the last one, the form "{n}" means the last character is repeated n times. Since it is preceded by ".", it means any n characters.)






share|improve this answer


























  • Thanks.That worked great if the pattern was at the beginning. Would it be possible to do something similar if the pattern was in the middle or end of a entry? For example row 5 in the dataset above.

    – Sarah
    Nov 23 '18 at 14:39











  • see the edited answer above: just replace the first argument of the grepl with either option.

    – iod
    Nov 23 '18 at 16:12











  • @Sarah don't forget to upvote and accept!

    – iod
    Nov 23 '18 at 16:29











  • @ iod. Just wondering is it possible to generalize the statement. For example I tried apply(data,1,function(x) grepl(paste0('rep(.,n)+',x["V1"],'...+'),x["V3"])). So that I can specify the position outside the function.

    – Sarah
    Nov 23 '18 at 17:09













  • Not sure what you're trying to achieve with that rep() call. As noted in the comments to your question - it's a good idea to familiarize yourself with regex syntax.

    – iod
    Nov 23 '18 at 17:19
















0














apply(df,1,function(x) grepl(paste0("^",x["data.V1"]),x["data.V2"]))


For every row (that's the 1 in apply), this will check whether the contents of data.V1 appears right at the beginning of data.V2 (^ means the beginning of a string for regular expressions).



Result:



    1     2     3     4     5     6 
TRUE TRUE FALSE TRUE FALSE FALSE


Replace the grepl first argument with the following for:



End of string: paste0(x["data.V1"],"$")
Middle of string: paste0(".+",x["data.V1"],".+")
After n characters (n defined elsewhere): paste0(".{",n,"}",x["data.V1"])


(for the last one, the form "{n}" means the last character is repeated n times. Since it is preceded by ".", it means any n characters.)






share|improve this answer


























  • Thanks.That worked great if the pattern was at the beginning. Would it be possible to do something similar if the pattern was in the middle or end of a entry? For example row 5 in the dataset above.

    – Sarah
    Nov 23 '18 at 14:39











  • see the edited answer above: just replace the first argument of the grepl with either option.

    – iod
    Nov 23 '18 at 16:12











  • @Sarah don't forget to upvote and accept!

    – iod
    Nov 23 '18 at 16:29











  • @ iod. Just wondering is it possible to generalize the statement. For example I tried apply(data,1,function(x) grepl(paste0('rep(.,n)+',x["V1"],'...+'),x["V3"])). So that I can specify the position outside the function.

    – Sarah
    Nov 23 '18 at 17:09













  • Not sure what you're trying to achieve with that rep() call. As noted in the comments to your question - it's a good idea to familiarize yourself with regex syntax.

    – iod
    Nov 23 '18 at 17:19














0












0








0







apply(df,1,function(x) grepl(paste0("^",x["data.V1"]),x["data.V2"]))


For every row (that's the 1 in apply), this will check whether the contents of data.V1 appears right at the beginning of data.V2 (^ means the beginning of a string for regular expressions).



Result:



    1     2     3     4     5     6 
TRUE TRUE FALSE TRUE FALSE FALSE


Replace the grepl first argument with the following for:



End of string: paste0(x["data.V1"],"$")
Middle of string: paste0(".+",x["data.V1"],".+")
After n characters (n defined elsewhere): paste0(".{",n,"}",x["data.V1"])


(for the last one, the form "{n}" means the last character is repeated n times. Since it is preceded by ".", it means any n characters.)






share|improve this answer















apply(df,1,function(x) grepl(paste0("^",x["data.V1"]),x["data.V2"]))


For every row (that's the 1 in apply), this will check whether the contents of data.V1 appears right at the beginning of data.V2 (^ means the beginning of a string for regular expressions).



Result:



    1     2     3     4     5     6 
TRUE TRUE FALSE TRUE FALSE FALSE


Replace the grepl first argument with the following for:



End of string: paste0(x["data.V1"],"$")
Middle of string: paste0(".+",x["data.V1"],".+")
After n characters (n defined elsewhere): paste0(".{",n,"}",x["data.V1"])


(for the last one, the form "{n}" means the last character is repeated n times. Since it is preceded by ".", it means any n characters.)







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 17:41

























answered Nov 23 '18 at 14:16









iodiod

4,3562724




4,3562724













  • Thanks.That worked great if the pattern was at the beginning. Would it be possible to do something similar if the pattern was in the middle or end of a entry? For example row 5 in the dataset above.

    – Sarah
    Nov 23 '18 at 14:39











  • see the edited answer above: just replace the first argument of the grepl with either option.

    – iod
    Nov 23 '18 at 16:12











  • @Sarah don't forget to upvote and accept!

    – iod
    Nov 23 '18 at 16:29











  • @ iod. Just wondering is it possible to generalize the statement. For example I tried apply(data,1,function(x) grepl(paste0('rep(.,n)+',x["V1"],'...+'),x["V3"])). So that I can specify the position outside the function.

    – Sarah
    Nov 23 '18 at 17:09













  • Not sure what you're trying to achieve with that rep() call. As noted in the comments to your question - it's a good idea to familiarize yourself with regex syntax.

    – iod
    Nov 23 '18 at 17:19



















  • Thanks.That worked great if the pattern was at the beginning. Would it be possible to do something similar if the pattern was in the middle or end of a entry? For example row 5 in the dataset above.

    – Sarah
    Nov 23 '18 at 14:39











  • see the edited answer above: just replace the first argument of the grepl with either option.

    – iod
    Nov 23 '18 at 16:12











  • @Sarah don't forget to upvote and accept!

    – iod
    Nov 23 '18 at 16:29











  • @ iod. Just wondering is it possible to generalize the statement. For example I tried apply(data,1,function(x) grepl(paste0('rep(.,n)+',x["V1"],'...+'),x["V3"])). So that I can specify the position outside the function.

    – Sarah
    Nov 23 '18 at 17:09













  • Not sure what you're trying to achieve with that rep() call. As noted in the comments to your question - it's a good idea to familiarize yourself with regex syntax.

    – iod
    Nov 23 '18 at 17:19

















Thanks.That worked great if the pattern was at the beginning. Would it be possible to do something similar if the pattern was in the middle or end of a entry? For example row 5 in the dataset above.

– Sarah
Nov 23 '18 at 14:39





Thanks.That worked great if the pattern was at the beginning. Would it be possible to do something similar if the pattern was in the middle or end of a entry? For example row 5 in the dataset above.

– Sarah
Nov 23 '18 at 14:39













see the edited answer above: just replace the first argument of the grepl with either option.

– iod
Nov 23 '18 at 16:12





see the edited answer above: just replace the first argument of the grepl with either option.

– iod
Nov 23 '18 at 16:12













@Sarah don't forget to upvote and accept!

– iod
Nov 23 '18 at 16:29





@Sarah don't forget to upvote and accept!

– iod
Nov 23 '18 at 16:29













@ iod. Just wondering is it possible to generalize the statement. For example I tried apply(data,1,function(x) grepl(paste0('rep(.,n)+',x["V1"],'...+'),x["V3"])). So that I can specify the position outside the function.

– Sarah
Nov 23 '18 at 17:09







@ iod. Just wondering is it possible to generalize the statement. For example I tried apply(data,1,function(x) grepl(paste0('rep(.,n)+',x["V1"],'...+'),x["V3"])). So that I can specify the position outside the function.

– Sarah
Nov 23 '18 at 17:09















Not sure what you're trying to achieve with that rep() call. As noted in the comments to your question - it's a good idea to familiarize yourself with regex syntax.

– iod
Nov 23 '18 at 17:19





Not sure what you're trying to achieve with that rep() call. As noted in the comments to your question - it's a good idea to familiarize yourself with regex syntax.

– iod
Nov 23 '18 at 17:19




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53448236%2fidentifying-if-one-field-is-partial-derived-from-another-in-r%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

Tangent Lines Diagram Along Smooth Curve

Yusuf al-Mu'taman ibn Hud

Zucchini