r - Remove a character from elements in a dataframe -
i have set of data elements preceded "<" , need remove "<" can perform data analysis. data saved in .txt file , i'm bringing r using read.table. below example of text file looks like.
background: 18 <10 27 22 <3 site: 30 44 23 <16 13
i used x=read.file
make dataframe, tried gsub("<","",x)
remove "<"
, result unexpected, @ least me. result.
[1] "1:2" "c(18, 30)" "1:2" "c(27, 23)" "c(2, 1)" "1:2"
i have no idea means or why it's happening. appreciate explanation both of going on here, , how should go accomplishing goal.
df <- read.table(header = true, text = "background site 18 30 <10 44 27 23 22 <16 <3 13", stringsasfactors = false)
you can use mutate_at
, apply gsub
function variables (i.e. background
, site
) wish remove preceding <
sign.
library(dplyr) df %>% mutate_at(vars(background, site), funs(as.numeric(gsub("^<", "", .))))
the output is:
background site 1 18 30 2 10 44 3 27 23 4 22 16 5 3 13
Comments
Post a Comment