Archive for May, 2017

May
24

R important things

R has no block comment, but you can still do the following things:

  • In RStudio: Ctrl+Shift+C
  • if(false){xxx} block

 

python to R

df.tail() -> tail(df)

dropna(df) ->na.omit(df) / df[complete.cases(df),]

df.describe() -> summary(df), library(psych): describe(df)

df.shape -> dim(df)

May
01

Two ways to update docs in cursor objects with pymongo

I have a collection with:

name    weight    amount
—————————————-
apple     2               3
banana  2               5
pear       2               5

I want add a column about the total price, which is equal to weight * amount

name    weight    amount    total
—————————————————-
apple     2               3              6
banana  2               5             10
pear       2               5            10


myFile = [
{"name":"apple", "weight":2, "amount":3}, 
{"name":"banana", "weight":2, "amount":5}, 
{"name":"pear", "weight":2, "amount":5}
]

testDB.db.testCollection.delete_many({})
testDB.db.testCollection.insert_many(myFile)

Method 1: collection.find() + update_one()

fruits = testDB.db.testCollection.find({}, modifiers={"$snapshot": True})

for fruit in fruits:
    total = fruit['weight'] * fruit['amount']
    testDB.db.testCollection.update_one({"_id":fruit['_id']}, {'$set': {'total': total}})

# check the result:
list(testDB.db.testCollection.find())</pre>
<pre>

 

Method 2: collection.aggregate() with $project, $multiply and $out


testDB.db.testCollection.aggregate([{'$project': {
        'name':1,
        'weight':1,
        'total': {"$multiply": ["$weight",'$amount']}
      }}, {
        '$out':'testCollectioncopy'
      }])
# check the result
list(testDB.db.testCollectioncopy.find())