Home > Back-end >  Using gsub in R to rename values in column
Using gsub in R to rename values in column

Time:10-06

I have the following dataframe:

structure(list(ID = c("D:/Desktop/BAS1/FA/FPT_LEFT_stats_FA_33714.txt", 
"D:/Desktop/BAS1/FA/FPT_LEFT_stats_FA_35377.txt", "D:/Desktop/BAS1/FA/FPT_LEFT_stats_FA_38623.txt", 
"D:/Desktop/BAS1/FA/FPT_LEFT_stats_FA_38806.txt", "D:/Desktop/BAS1/FA/FPT_LEFT_stats_FA_39593.txt", 
"D:/Desktop/BAS1/FA/FPT_LEFT_stats_FA_39820.txt"), mean = c(0.374389, 
0.368736, 0.397192, 0.483207, 0.372074, 0.367745), median = c(0.374017, 
0.353222, 0.390063, 0.49433, 0.323613, 0.344656), std = c("0.110854", 
"0.144632", "0.128366", "0.106284", "0.145565", "0.178729"), 
    min = c(0.149913, 0.108011, 0.10917, 0.235377, 0.157571, 
    0.0690003), max = c(0.650852, 0.692352, 0.672184, 0.658852, 
    0.723939, 0.707643), count = c(75L, 75L, 64L, 62L, 108L, 
    42L)), row.names = c(NA, 6L), class = "data.frame")

which looks like:

                                              ID     mean   median      std       min      max count
1 D:/Desktop/BAS1/FA/FPT_LEFT_stats_FA_33714.txt 0.374389 0.374017 0.110854 0.1499130 0.650852    75
2 D:/Desktop/BAS1/FA/FPT_LEFT_stats_FA_35377.txt 0.368736 0.353222 0.144632 0.1080110 0.692352    75
3 D:/Desktop/BAS1/FA/FPT_LEFT_stats_FA_38623.txt 0.397192 0.390063 0.128366 0.1091700 0.672184    64
4 D:/Desktop/BAS1/FA/FPT_LEFT_stats_FA_38806.txt 0.483207 0.494330 0.106284 0.2353770 0.658852    62
5 D:/Desktop/BAS1/FA/FPT_LEFT_stats_FA_39593.txt 0.372074 0.323613 0.145565 0.1575710 0.723939   108
6 D:/Desktop/BAS1/FA/FPT_LEFT_stats_FA_39820.txt 0.367745 0.344656 0.178729 0.0690003 0.707643    42

I would like to remove all non-numeric items from the ID column.

When I use gsub("[^0-9]", "", DF[1,1]) it adds a leading 1 to the output. I am not sure why.

I would ultimately like to apply this function to all the rows in the ID column and keep the numeric items as file ID.

So desired output:

          ID     mean   median      std       min      max count
1 33714      0.374389 0.374017 0.110854 0.1499130 0.650852    75
2 35377      0.368736 0.353222 0.144632 0.1080110 0.692352    75
.
.
.
.

CodePudding user response:

Tell me if this is what you're looking for

df$ID <- gsub("[^0-9]", "", df$ID)

          ID     mean   median      std       min      max count
1 1234567890 0.374389 0.374017 0.110854 0.1499130 0.650852    75
2 1234567890 0.368736 0.353222 0.144632 0.1080110 0.692352    75
3 1234567890 0.397192 0.390063 0.128366 0.1091700 0.672184    64
4 1234567890 0.483207 0.494330 0.106284 0.2353770 0.658852    62
5 1234567890 0.372074 0.323613 0.145565 0.1575710 0.723939   108
6 1234567890 0.367745 0.344656 0.178729 0.0690003 0.707643    42

CodePudding user response:

Using str_extract

df$ID <- str_extract(df$ID, "(\\d) ")
df
          ID     mean   median      std       min      max count
1 1234567890 0.374389 0.374017 0.110854 0.1499130 0.650852    75
2 1234567890 0.368736 0.353222 0.144632 0.1080110 0.692352    75
3 1234567890 0.397192 0.390063 0.128366 0.1091700 0.672184    64
4 1234567890 0.483207 0.494330 0.106284 0.2353770 0.658852    62
5 1234567890 0.372074 0.323613 0.145565 0.1575710 0.723939   108
6 1234567890 0.367745 0.344656 0.178729 0.0690003 0.707643    42

CodePudding user response:

Or using specific regex pattern based on your data. Here is a base R way using sub.

df$ID <- sub('.*_(\\d )\\.txt', '\\1', df$ID)
df

#     ID     mean   median      std       min      max count
#1 33714 0.374389 0.374017 0.110854 0.1499130 0.650852    75
#2 35377 0.368736 0.353222 0.144632 0.1080110 0.692352    75
#3 38623 0.397192 0.390063 0.128366 0.1091700 0.672184    64
#4 38806 0.483207 0.494330 0.106284 0.2353770 0.658852    62
#5 39593 0.372074 0.323613 0.145565 0.1575710 0.723939   108
#6 39820 0.367745 0.344656 0.178729 0.0690003 0.707643    42
  • Related