I have two data frames
df1 # a b # 1 10 20 # 2 11 21 # 3 12 22 # 4 13 23 # 5 14 24 # 6 15 25 df2 # a b # 1 4 8
I need the following output:
df3 # a b # 1 14 28 # 2 15 29 # 3 16 30 # 4 17 31 # 5 18 32 # 6 19 33
i.e. add df2 to each line of df1.
Is there a way to get the desired result using plyr (mdplyr ??) or dplyr?
I see no reason for "dplyr" for anything like that. In the R database, you can simply do:
df1 + unclass(df2) # a b # 1 14 28 # 2 15 29 # 3 16 30 # 4 17 31 # 5 18 32 # 6 19 33
The same as df1 + list(4, 8).
df1 + list(4, 8)
One liner with dplyr.
mutate_each(df1, funs(.+ df2$.), a:b) # a b #1 14 28 #2 15 29 #3 16 30 #4 17 31 #5 18 32 #6 19 33
Basic solution Rusing a sweet function sweep:
R
sweep
sweep(df1, 2, unlist(df2), '+') # a b #1 14 28 #2 15 29 #3 16 30 #4 17 31 #5 18 32 #6 19 33
Source: https://habr.com/ru/post/1584615/More articles:Java UDP connection - javaCall a function from a div with an angular parameter JS - javascriptLaunch node-inspector and node-debug with npm launch - node.jsExcel data as a data grid in vb6 - excelHow can I find out when MongoDB recovery is over - mongodbWCF is hosted in WPF and how can I change the controls in MainWindow UI from wcf? - multithreadingКак я могу получить доступ к новому ключевому слову javascript из scala.js? - javascriptNeo4J: How to Find Unique Nodes from a Set of Paths - neo4jUITableView shows all rows and without scrolling - iosPython random place generator with memory - pythonAll Articles