strsplit、mapply、paste、match函数

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函数)