首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >R中字段抽取、字段合并、字段匹配

R中字段抽取、字段合并、字段匹配

作者头像
Erin
发布2018-01-09 16:35:45
5.2K0
发布2018-01-09 16:35:45
举报
文章被收录于专栏:大数据风控大数据风控

1、字段抽取

字段抽取,是根据已知列数据的开始和结束位置,抽取出新的列

字段截取函数:substr(x,start,stop)

tel <- '18922254812';
#运营商
band <- substr(tel, 1, 3)
#地区
area <- substr(tel, 4, 7)
#号码段
num <- substr(tel, 8, 11)

tels <- read.csv('1.csv');
#运营商
bands <- substr(tels[,1], 1, 3)
#地区
areas <- substr(tels[,1], 4, 7)
#号码段
nums <- substr(tels[,1], 8, 11)

new_tels <- data.frame(tels, bands, areas, nums)

2、字段合并

字段合并,是指将同一个数据框中的不同列,进行合并,形成新的列

字符分割函数:paste(x1,x2,...,sep=" ")

data <- read.table('1.csv', sep=' ')

p_data <- paste(data[,1], data[,2], data[,3], sep="")

newData <- data.frame(data, p_data)

3、记录合并

将两个结构相同的数据框,合并成一个数据框

记录合并函数:rbind(dataFrame1,dataFrame2,...)

data_1_1 <- read.table('1.csv', sep='|', header=TRUE, fileEncoding='utf-8');
data_1_2 <- read.table('2.csv', sep='|', header=TRUE, fileEncoding='utf-8');
data_1_3 <- read.table('3.csv', sep='|', header=TRUE, fileEncoding='utf-8');

data <- rbind(data_1_1, data_1_2, data_1_3)
fix(data)

4、字段匹配

将不同结构的数据框,按照一定的条件进行合并(两表合并)

字段匹配函数:merge(x,y,by.x,by.y)

items <- read.table('1.csv', sep='|', header=FALSE, fileEncoding='utf-8')
fix(items)

prices <- read.table('2.csv', sep='|', header=FALSE, fileEncoding='utf-8')
fix(prices)

itemPrices <- merge(prices, items, by.x=c('V1'), by.y=c('V1'))
fix(itemPrices)

Join( )也可以用来实现两表连接:

inner_join(t1,t2,by=c("列名1","列名2"))
#功能等于:
merge(t1,t2,by.x="列名",by.y="列名")

#还有其他的join方式:
full_join 全连接
left_join 左连接
right_join 右连接

5、字符串处理高级技巧

x <- c("Hellow", "World", "!") 
#一、字符串长度
nchar(x)
#[1] 6 5 1
length(x)
#[1] 3

#二、字符串替换
chartr("HW", "ZX", x)
#[1] "Zellow" "Xorld"  "!"     

#三、字符串的大小写转换
tolower(x)
#[1] "hellow" "world"  "!"
toupper(x)
#[1] "HELLOW" "WORLD"  "!"     

#四、字符串的拼接
paste("CK", 1:6, sep="")
#[1] "CK1" "CK2" "CK3" "CK4" "CK5" "CK6" 
x <- list(a="aaa", b="bbb", c="ccc") 
y <- list(d=1, e=2) 
paste(x, y, sep="-")     
#较短的向量被循环使用 
#[1] "aaa-1" "bbb-2" "ccc-1" 

#五、字符串切割
text <- "Hello word!"
strsplit(text, ' ')
#[[1]]
#[1] "Hello" "word!"
class(strsplit(text, ' '))
#[1] "list"

#有一种情况很特殊:
#如果split参数的字符长度为0,得到的结果就是一个个的字符:
strsplit(text, '')
#[[1]]
# [1] "H" "e" "l" "l" "o" " " "w" "o" "r" "d" "!"

#一个首字符大写的综合案例
capStringAll <- function(x)
  {
  s <- strsplit(x, " ")[[1]]
  paste(toupper(substring(s, 1, 1)), substring(s, 2),
        sep = "", collapse = " ")
}
capStringAll("hello word")
#[1] "Hello Word"

capString <- function(x) 
  {
  s <- strsplit(x, " ")[[1]]
  s[1] <- paste(toupper(substring(s[1], 1, 1)), substring(s[1], 2), sep = "", collapse = " ");
  paste(s, sep = "", collapse = " ")
}
capString("hello word")
#[1] "Hello word"

#六、字符串的查找
#grep, grepl: 返回pattern的匹配项。
#前者返回匹配项目的下标;后者返回逻辑值,x长度有多少,就返回多少个逻辑值。
#如果添加一个value参数,赋值为T,则返回匹配项的值。
text <- c("Company", "Coworker", "Cooperation", "Can")
grep("o", text)
#[1] 1 2 3

grepl("o", text)
#[1]  TRUE  TRUE  TRUE FALSE

grep("o", text, value = T)
#[1] "Company"     "Coworker"    "Cooperation"

#七、字符串的替换
#sub, gsub: 返回用replacement替换匹配项之后的x(字符型向量)。
#前者只替换向量中每个元素的第一个匹配值,后者替换所有匹配值。
#注意以下两个例子中"o"的替换方式。
sub("o", "xx", text)
#[1] "Cxxmpany"     "Cxxworker"    "Cxxoperation" "Can" 
gsub("o", "xx", text)
#[1] "Cxxmpany"     "Cxxwxxrker"   "Cxxxxperatixxn" "Can"

#八、字符串的截取
x <- "123456789" 
substr(x, 2, 4) 
#[1] "234" 
substring(x, c(2,4), c(4,5,8)) 
#[1] "234"     "45"      "2345678" 
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017年07月11日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 1、字段抽取
  • 2、字段合并
  • 3、记录合并
  • 4、字段匹配
  • 5、字符串处理高级技巧
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档