R 从数据帧中的元素中删除字符
我有一组数据,其中一些元素前面有“ 输出为:R 从数据帧中的元素中删除字符,r,dataframe,gsub,R,Dataframe,Gsub,我有一组数据,其中一些元素前面有“ 输出为: Background Site 1 18 30 2 10 44 3 27 23 4 22 16 5 3 13 使用readLines读取文件,执行gsub,然后使用Read.table重新读取文件。不使用包: read.table(text = gsub("<", "", readLines("myfile")), as.is = TRU
Background Site
1 18 30
2 10 44
3 27 23
4 22 16
5 3 13
使用
readLines
读取文件,执行gsub
,然后使用Read.table
重新读取文件。不使用包:
read.table(text = gsub("<", "", readLines("myfile")), as.is = TRUE)
gsub
对数据不起作用。frame
直接-x[]看起来数据更像x
df <- read.table(header = TRUE, text = "Background Site
18 30
<10 44
27 23
22 <16
<3 13", stringsAsFactors = FALSE)
library(dplyr)
df %>% mutate_at(vars(Background, Site),
funs(as.numeric(gsub("^<", "", .))))
Background Site
1 18 30
2 10 44
3 27 23
4 22 16
5 3 13
read.table(text = gsub("<", "", readLines("myfile")), as.is = TRUE)
clean <- function(x) as.numeric(gsub(">", "", x))
DF[-1] <- lapply(DF[-1], clean)