如何使用R删除data.frame中特定列中的字符?

时间:2019-05-24 13:04:19

标签: r string dataframe

我有几个data.frame的列表> tail(results[[1]]) var1 var2 corr 4945 UniRef90_A0A075GGL3 UniRef90_A0A075GGW4 -0.12058932 4946 UniRef90_A0A075GGU1 UniRef90_A0A075GGW4 -0.01740142 4947 UniRef90_A0A075GGU4 UniRef90_A0A075GGW4 0.16400148 4948 UniRef90_A0A075GGV0 UniRef90_A0A075GGW4 -0.09698018 4949 UniRef90_A0A075GGV1 UniRef90_A0A075GGW4 0.22409572 4950 UniRef90_A0A075GGV8 UniRef90_A0A075GGW4 0.43184873 > tail(results[[2]]) var1 var2 corr 4945 UniRef90_A0A075GJW0 UniRef90_A0A075GKB8 -0.1059095 4946 UniRef90_A0A075GJW5 UniRef90_A0A075GKB8 -0.4336370 4947 UniRef90_A0A075GJX5 UniRef90_A0A075GKB8 -0.1875841 4948 UniRef90_A0A075GJY4 UniRef90_A0A075GKB8 0.2658149 4949 UniRef90_A0A075GJY8 UniRef90_A0A075GKB8 -0.2820792 4950 UniRef90_A0A075GJY9 UniRef90_A0A075GKB8 -0.2402827 (每个data.frame有3列)。看起来像这样:

UniRef90_

我将这些data.frames绑定为一个。但这会提供巨大的data.frame。这就是为什么我想在绑定之前删除列var1var2中的字符串Set QS = ActivePresentation.Designs(2).SlideMaster.CustomLayouts(2) Dim Seconds As Integer Seconds = 30 QS.Shapes("Counter").TextFrame.TextRange = Seconds For i = 1 To 30 Dim WAIT As Double WAIT = Timer While Timer < WAIT + 1 DoEvents Wend Seconds = Seconds - 1 QS.Shapes("Counter").TextFrame.TextRange = Seconds Next i 以减小大小。

有帮助吗?

2 个答案:

答案 0 :(得分:1)

您可以在弯曲数据框之前在var1和var2上尝试此操作。

sub("UniRef90_","", dataframe$yourvariable)

答案 1 :(得分:0)

我们可以遍历str_remove,并用library(tidyverse) map_df(results, ~ .x %>% mutate_at(vars(matches('^var\\d+$')), list(~ str_remove(., "^UniRef90_")))) # var1 var2 corr #1 A0A075GGL3 A0A075GGW4 -0.12058932 #2 A0A075GGU1 A0A075GGW4 -0.01740142 #3 A0A075GGU4 A0A075GGW4 0.16400148 #4 A0A075GGV0 A0A075GGW4 -0.09698018 #5 A0A075GGV1 A0A075GGW4 0.22409572 #6 A0A075GGV8 A0A075GGW4 0.43184873 #7 A0A075GJW0 A0A075GKB8 -0.10590950 #8 A0A075GJW5 A0A075GKB8 -0.43363700 #9 A0A075GJX5 A0A075GKB8 -0.18758410 #10 A0A075GJY4 A0A075GKB8 0.26581490 #11 A0A075GJY8 A0A075GKB8 -0.28207920 #12 A0A075GJY9 A0A075GKB8 -0.24028270 results <- list(structure(list(var1 = c("UniRef90_A0A075GGL3", "UniRef90_A0A075GGU1", "UniRef90_A0A075GGU4", "UniRef90_A0A075GGV0", "UniRef90_A0A075GGV1", "UniRef90_A0A075GGV8"), var2 = c("UniRef90_A0A075GGW4", "UniRef90_A0A075GGW4", "UniRef90_A0A075GGW4", "UniRef90_A0A075GGW4", "UniRef90_A0A075GGW4", "UniRef90_A0A075GGW4"), corr = c(-0.12058932, -0.01740142, 0.16400148, -0.09698018, 0.22409572, 0.43184873)), class = "data.frame", row.names = c("4945", "4946", "4947", "4948", "4949", "4950")), structure(list(var1 = c("UniRef90_A0A075GJW0", "UniRef90_A0A075GJW5", "UniRef90_A0A075GJX5", "UniRef90_A0A075GJY4", "UniRef90_A0A075GJY8", "UniRef90_A0A075GJY9"), var2 = c("UniRef90_A0A075GKB8", "UniRef90_A0A075GKB8", "UniRef90_A0A075GKB8", "UniRef90_A0A075GKB8", "UniRef90_A0A075GKB8", "UniRef90_A0A075GKB8"), corr = c(-0.1059095, -0.433637, -0.1875841, 0.2658149, -0.2820792, -0.2402827)), class = "data.frame", row.names = c("4945", "4946", "4947", "4948", "4949", "4950"))) 删除子字符串

{{1}}

数据

{{1}}