Reputation: 949
I want to change specific names within a list, using a condition
Here is the list of string items
varnames <- c("var1_L1","var2_L1","var3_L1", "var4_L1", "Coefs_L1", "Pval_L1")
I would like to remove everything to the right of "_" but only for items that have "var" in the text
This will remove everything for all items
sub("_.*","", varnames)
I've tried to use an if condition with this but couldn't get it to work.
If it helps, this would be what the end result looks like
varnames_result <- c("var1","var2","var3", "var4", "Coefs_L1", "Pval_L1")
Any help greatly appreciated. Thanks
Upvotes: 0
Views: 22
Reputation: 388982
You can use posiitve lookbehind regex to remove everything that follows 'var'
+ number.
sub('(?<=var\\d).*', '', varnames, perl = TRUE)
#[1] "var1" "var2" "var3" "var4" "Coefs_L1" "Pval_L1"
Upvotes: 1
Reputation: 79228
You could do:
sub("(var.*?)_.*", "\\1", varnames)
[1] "var1" "var2" "var3" "var4" "Coefs_L1" "Pval_L1"
If there is only one _
then (var.*)_.*
is sufficient
Upvotes: 2