美文网首页
strsplit、mapply、paste、match函数

strsplit、mapply、paste、match函数

作者: 小梦游仙境 | 来源:发表于2019-08-09 22:03 被阅读0次

strsplit函数

ids
a<-strsplit(as.character(ids[,2]), " /// ")#a就变成列表,ids有多少行,就生成多少列表
a

mapply函数

tmp <- mapply( cbind, ids[,1], a ) #将ids和a按列合并
tmp

strsplit函数

> x = c("abcde", "ghij", "klmnopq")
> strsplit(x, "", fixed=TRUE)
[[1]]
[1] "a" "b" "c" "d" "e"

[[2]]
[1] "g" "h" "i" "j"

[[3]]
[1] "k" "l" "m" "n" "o" "p" "q"

mapply函数

> mapply(sum, list(a=1,b=2,c=3), list(a=10,b=20,d=30))
 a  b  c 
11 22 33 
> mapply(function(x,y) x^y, c(1:5), c(1:5))
[1]    1    4   27  256 3125
> mapply(function(x,y) c(x+y, x^y), c(1:5), c(1:5))
     [,1] [,2] [,3] [,4] [,5]
[1,]    2    4    6    8   10
[2,]    1    4   27  256 3125

paste函数

> paste(1:12, c("st", "nd", "rd", rep("th", 9)),sep = "",collapse = ",")
[1] "1st,2nd,3rd,4th,5th,6th,7th,8th,9th,10th,11th,12th"
> paste(1:12, c("st", "nd", "rd", rep("th", 9)))
 [1] "1 st"  "2 nd"  "3 rd"  "4 th"  "5 th"  "6 th"  "7 th" 
 [8] "8 th"  "9 th"  "10 th" "11 th" "12 th"
> paste0(1:12)
 [1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10" "11"
[12] "12"
> ## When passing a single vector, paste0 and paste work like as.character.
> paste0(1:12)
 [1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10" "11" "12"
> paste(1:12)        # same
 [1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10" "11" "12"
> as.character(1:12) # same
 [1] "1"  "2"  "3"  "4"  "5"  "6"  "7"  "8"  "9"  "10" "11" "12"
> ## If you pass several vectors to paste0, they are concatenated in a
> ## vectorized way.
> (nth <- paste0(1:12, c("st", "nd", "rd", rep("th", 9))))
 [1] "1st"  "2nd"  "3rd"  "4th"  "5th"  "6th"  "7th"  "8th"  "9th"  "10th" "11th" "12th"
> ## paste works the same, but separates each input with a space.
> ## Notice that the recycling rules make every input as long as the longest input.
> paste(month.abb, "is the", nth, "month of the year.")
 [1] "Jan is the 1st month of the year."  "Feb is the 2nd month of the year." 
 [3] "Mar is the 3rd month of the year."  "Apr is the 4th month of the year." 
 [5] "May is the 5th month of the year."  "Jun is the 6th month of the year." 
 [7] "Jul is the 7th month of the year."  "Aug is the 8th month of the year." 
 [9] "Sep is the 9th month of the year."  "Oct is the 10th month of the year."
[11] "Nov is the 11th month of the year." "Dec is the 12th month of the year."
> paste(month.abb, letters)
 [1] "Jan a" "Feb b" "Mar c" "Apr d" "May e" "Jun f" "Jul g" "Aug h" "Sep i" "Oct j"
[11] "Nov k" "Dec l" "Jan m" "Feb n" "Mar o" "Apr p" "May q" "Jun r" "Jul s" "Aug t"
[21] "Sep u" "Oct v" "Nov w" "Dec x" "Jan y" "Feb z"
> ## You can change the separator by passing a sep argument
> ## which can be multiple characters.
> paste(month.abb, "is the", nth, "month of the year.", sep = "_*_")
 [1] "Jan_*_is the_*_1st_*_month of the year."  "Feb_*_is the_*_2nd_*_month of the year." 
 [3] "Mar_*_is the_*_3rd_*_month of the year."  "Apr_*_is the_*_4th_*_month of the year." 
 [5] "May_*_is the_*_5th_*_month of the year."  "Jun_*_is the_*_6th_*_month of the year." 
 [7] "Jul_*_is the_*_7th_*_month of the year."  "Aug_*_is the_*_8th_*_month of the year." 
 [9] "Sep_*_is the_*_9th_*_month of the year."  "Oct_*_is the_*_10th_*_month of the year."
[11] "Nov_*_is the_*_11th_*_month of the year." "Dec_*_is the_*_12th_*_month of the year."
> ## To collapse the output into a single string, pass a collapse argument.
> paste0(nth, collapse = ", ")
[1] "1st, 2nd, 3rd, 4th, 5th, 6th, 7th, 8th, 9th, 10th, 11th, 12th"
> ## For inputs of length 1, use the sep argument rather than collapse
> paste("1st", "2nd", "3rd", collapse = ", ") # probably not what you wanted
[1] "1st 2nd 3rd"
> paste("1st", "2nd", "3rd", sep = ", ")
[1] "1st, 2nd, 3rd"
> ## You can combine the sep and collapse arguments together.
> paste(month.abb, nth, sep = ": ", collapse = "; ")
[1] "Jan: 1st; Feb: 2nd; Mar: 3rd; Apr: 4th; May: 5th; Jun: 6th; Jul: 7th; Aug: 8th; Sep: 9th; Oct: 10th; Nov: 11th; Dec: 12th"

match函数

user_id<-c('A','B','C','D')
u1<-c('B','C','D','A')
e1<-c('a1','a2','a3','a4')
m1<-data.frame(u1,e1)
t1<-match(user_id,m1$u1)
t1
[1] 4 1 2 3
x<-m1[match(user_id,m1$u1),]
x
  u1 e1
4  A a4
1  B a1
2  C a2
3  D a3

第一个t1得到的是user_id在m1$u1里面的坐标位置,也就是说接下来只能是从m1里取子集,取出来的子集的顺序是按user_id的顺序来的。

相关文章

  • strsplit、mapply、paste、match函数

    strsplit函数 mapply函数 strsplit函数 mapply函数 paste函数 match函数 第...

  • 常见函数入门

    1.字符串操作函数 paste()连接字符串 strsplit()将字符串分开 2.基础函数 rbind() 按...

  • 分割,合并

    01、分割 strsplit函数:字符串分割函数v <- strsplit(u,"...") #用...将字符分开...

  • mapply函数

    apply函数系列是R面向数组设计的体现,也是避免for循环的神器。apply函数簇是一系列功能类似对数组或者列表...

  • R,笔记01

    设置随机数的种子值 grep用法 载入数据 创建数据框 subset用法 strsplit 和 paste用法

  • R语言与生信应用24-R语法-mapply

    mapply mapply 称为多变量(multivariate)apply可以接收指定函数的一组参数并行计算。 ...

  • apply、lapply、sapply、mapply、tappl

    简介批量处理函数有很重要的apply族函数:lapply sapply apply tapply mapply。这...

  • strsplit函数的使用

    函数strsplit用于按照指定的字符把长的字符串分割成短的子字符串, strsplit(x, split, fi...

  • paste函数

    x=[1:5] y=letters(1:5) paste(y,x) 结果:"a 1" "b 2" "c 3" "d...

  • 学习office——Excel之Match与Index函数

    一、Match与Index函数 1、函数语法 Match函数:负责查找MATCH(lookup_value,loo...

网友评论

      本文标题:strsplit、mapply、paste、match函数

      本文链接:https://www.haomeiwen.com/subject/akbkjctx.html