R:用两个元素折叠向量

时间:2015-08-19 15:57:16

标签: r paste collapse n-gram

我有一个载体,例如sdata = c('a', 'b', 'c', 'd')

sdata
[1] "a" "b" "c" "d"

如何通过两个(或更多,如果需要)元素将其折叠以获得以下输出?

desiredOutput 
[1] "a b" "b c" "c d"

谢谢!

编辑:真实样本数据:

sdata = list(c("salmon", "flt", "atl", "farm", "wild", "per", "lb", "or", 
"fam", "pk"), c("vit", "min", "adult", "eye", "visit", "our", 
"pharmacy", "we", "accept", "express", "script", "offer", "val", 
"mathing", "pricing", "fast", "conv", "service"), c("ct", "gal", 
"or", "drawstring", "odor", "shield", "twist", "tie", "lawn", 
"leaf", "in", "plumber", "brush"))

 sdata
[[1]]
 [1] "salmon" "flt"    "atl"    "farm"   "wild"   "per"    "lb"     "or"     "fam"    "pk"    

[[2]]
 [1] "vit"      "min"      "adult"    "eye"      "visit"    "our"      "pharmacy" "we"       "accept"   "express"  "script"  
[12] "offer"    "val"      "mathing"  "pricing"  "fast"     "conv"     "service" 

[[3]]
 [1] "ct"         "gal"        "or"         "drawstring" "odor"       "shield"     "twist"      "tie"        "lawn"      
[10] "leaf"       "in"         "plumber"    "brush"     

1 个答案:

答案 0 :(得分:2)

我们可以删除' sdata'的最后一个元素和第一个元素。和paste长度相同的向量。

 paste(sdata[-length(sdata)], sdata[-1])
 #[1] "a b" "b c" "c d"

这也可以写成

 paste(head(sdata,-1), tail(sdata,-1))
 #[1] "a b" "b c" "c d"

更新

基于新的' sdata' (在list中),我们使用lapply循环遍历list元素并使用相同的代码

lapply(sdata, function(x) paste(head(x,-1), tail(x,-1)))
#[[1]]
#[1] "salmon flt" "flt atl"    "atl farm"   "farm wild"  "wild per"  
#[6] "per lb"     "lb or"      "or fam"     "fam pk"    

#[[2]]
# [1] "vit min"         "min adult"       "adult eye"       "eye visit"      
# [5] "visit our"       "our pharmacy"    "pharmacy we"     "we accept"      
# [9] "accept express"  "express script"  "script offer"    "offer val"      
#[13] "val mathing"     "mathing pricing" "pricing fast"    "fast conv"      
#[17] "conv service"   

#[[3]]
# [1] "ct gal"          "gal or"          "or drawstring"   "drawstring odor"
# [5] "odor shield"     "shield twist"    "twist tie"       "tie lawn"       
# [9] "lawn leaf"       "leaf in"         "in plumber"      "plumber brush"  

或者,在不使用匿名函数的情况下,删除paste

中的第一个和最后一个Map元素后,list可以sdata Map(paste, lapply(sdata, head, -1), lapply(sdata, tail, -1))
     MongoClient c =  new MongoClient();

     //to get your db (Please replace "yourDB" with your db name)
     MongoDatabase db = c.getDatabase("yourDB");

     //to get your collection (Please replace "yourColl" with your collection name)
     MongoCollection<Document> yourColl = db.getCollection("yourColl");

     //search result list to be return
     List<Document> result = new ArrayList<Document>();

     //to get final record from billList 
     yourColl.find().projection(new Document("billList", new Document("$slice",-1))).into(result);

     //Based on your snippet given, assume there is only 1 document return and to get that document. You can put a loop here if there are more than 1 documents return and to be update
     Document resultToUpdate = result.get(0);

     //Update status
     yourColl.updateOne(new Document("_id",resultToUpdate.get("_id")).append("billList.id", resultToUpdate.get("billList.id")), new Document("$set",new Document("billList.$.status",1)));