df and also I desire to develop a brand-new column based on existing columns. Something prefer this:

if (df<<"a">> == df<<"b">>) { df<<"c">> The difficulty is the the if condition is checked just for the first row... If I develop a function from the over if statement climate I use apply() (or mapply()...), that is the same.

You are watching: R add column to dataframe based on other columns

In Python/pandas I have the right to use this:

df<"c"> = df<<"a", "b">>.apply(lambda x: x<"a"> + x<"b"> if (x<"a"> == x<"b">) \ else x<"b"> - x<"a">, axis=1)I desire something similar in R. Therefore the an outcome should look like this:

a b c1 1 1 22 2 20 183 3 3 64 4 4 85 5 50 45
r dataframe apply
share
enhance this concern
monitor
edited Mar 9 "17 at 22:18

*

zx8754
43.3k1010 gold badges9999 silver badges160160 bronze title
asked Aug 26 "16 in ~ 11:33
*

rageszragesz
7,0661818 gold badges6666 silver badges8585 bronze title
2
add a comment |

5 answer 5


active earliest Votes
31
One option is ifelse i beg your pardon is vectorized version of if/else. If we space doing this because that each row, the if/else as verified in the OP"s pandas post can be done in one of two people a because that loop or lapply/sapply, yet that would be inefficient in R.

df This can be otherwise created as

df$c to produce the "c" shaft in the original dataset

As the OP desires a similar option in R using if/else

df$c
re-publishing
boost this price
monitor
edited Aug 26 "16 at 11:53
reply Aug 26 "16 at 11:34

*

akrunakrun
720k2626 yellow badges410410 silver badges518518 bronze badges
3
add a comment |
9
Here is a slightly much more confusing algebraic method:

df$c The idea is that the "minus" operator is turn on or off based on the check a==b.


re-publishing
enhance this price
monitor
answered Aug 26 "16 at 11:48
*

lmolmo
36.1k99 yellow badges4949 silver- badges5757 bronze badges
1
add a comment |
7
If you desire an apply method, climate another method with mapply would certainly be develop a role and apply it,

fun1
share
boost this answer
follow
reply Aug 26 "16 at 11:57

*

SotosSotos
44.8k55 yellow badges2929 silver- badges5959 bronze badges
include a comment |
5
Using dplyr package:

library(dplyr)df % mutate(c = if_else(a == b, a + b, b - a))df# a b c# 1 1 1 2# 2 2 20 18# 3 3 3 6# 4 4 4 8# 5 5 50 45
re-publishing
improve this price
monitor
answered Mar 9 "17 at 22:17
zx8754zx8754
43.3k1010 yellow badges9999 silver- badges160160 bronze badges
2
add a comment |
4
A solution with apply

myFunction
re-superstructure
boost this price
monitor
answered Aug 26 "16 at 11:55
PhannPhann
1,1321212 silver- badges2020 bronze badges
0
add a comment |

your Answer


thanks for contributing solution to stack Overflow!

Please be certain to answer the question. Carry out details and also share her research!

But avoid

Asking because that help, clarification, or responding to other answers.Making statements based on opinion; ago them increase with referrals or personal experience.

To find out more, check out our tips on writing good answers.

See more: How To Find Arctan Without A Calculator, Re: Arctan Without Calculator


Draft saved
Draft discarded

Sign increase or log in in


sign up utilizing Google
sign up utilizing Facebook
authorize up utilizing Email and Password
submit

Post as a guest


name
email Required, yet never shown


Post together a guest


surname
email

Required, yet never shown


post Your prize Discard

By click “Post her Answer”, friend agree to our regards to service, privacy policy and also cookie policy


Not the price you're feather for? Browse various other questions tagged r dataframe use or asking your own question.


The Overflow Blog
Featured on Meta
Linked
1
applying formula in R to multiple rows
0
Create brand-new variable in R based on existing variable
1096
Grouping functions (tapply, by, aggregate) and the *apply family
-6
Need to convert a obelisk or data come 0 or 1 in R
associated
1096
Grouping functions (tapply, by, aggregate) and the *apply family members
1171
produce a Pandas Dataframe by appending one row at a time
1187
Adding brand-new column come existing DataFrame in Python pandas
1738
Delete a column from a Pandas DataFrame
1068
just how to drop rows the Pandas DataFrame whose value in a specific column is NaN
1107
adjust column form in pandas
2986
exactly how to iterate end rows in a DataFrame in Pandas
2709
how do I choose rows from a DataFrame based on column values?
1190
acquire list from pandas DataFrame column headers
462
pandas create brand-new column based on values from various other columns / use a role of multiple columns, row-wise
warm Network questions an ext hot inquiries

inquiry feed
i ordered it to RSS
concern feed To i ordered it to this RSS feed, copy and also paste this URL right into your RSS reader.


lang-r
stack Overflow
commodities
company
ridge Exchange Network
site style / logo © 2021 stack Exchange Inc; user contributions license is granted under cc by-sa. Rev2021.10.1.40358


Stack Overflow works ideal with JavaScript allowed
*

her privacy

By click “Accept all cookies”, girlfriend agree ridge Exchange deserve to store cookie on your maker and disclose info in accordance with our Cookie Policy.