登录
首页精彩阅读R中字段抽取、字段合并、字段匹配
R中字段抽取、字段合并、字段匹配
2017-11-26
收藏

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

1、字段抽取

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

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

[python] view plain copy

    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=" ")

[python] view plain copy

    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,...)
[python] view plain copy

    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)

[python] view plain copy

    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( )也可以用来实现两表连接:

[python] view plain copy

    inner_join(t1,t2,by=c("列名1","列名2"))  
    #功能等于:  
    merge(t1,t2,by.x="列名",by.y="列名")  
      
    #还有其他的join方式:  
    full_join 全连接  
    left_join 左连接  
    right_join 右连接  

5、字符串处理高级技巧
[python] view plain copy

    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"  

数据分析咨询请扫描二维码

客服在线
立即咨询