calculate new column in dataframe or list using a function with column as param
up vote
1
down vote
favorite
I'm trying to calculate a new column with a user defined function that needs data from same row and a fixed value valid for all rows:
myfunc <- function(ds,colname,val1,col1,col2){
# content of new column <colname> should be computed from:
ds[colname] = val1 + ds[col1] * ds[col2] # for each row of ds
return(ds)
}
v1 = 2
data(mtcars)
mt = head(mtcars)
mt
mpg cyl disp hp drat wt qsec vs am gear
carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
apply(mt,'newcol',v1,mt$wt,mt$qsec)
mt
What I would like to see in mt$newcol in first row is: 2 + 2.620 * 16.46 (-> 45.12) and all other rows similiar.
So, how can I send a fixed value (v1) and two values from each row to my function and store returned value in this row in a new column?
Thanks
r
add a comment |
up vote
1
down vote
favorite
I'm trying to calculate a new column with a user defined function that needs data from same row and a fixed value valid for all rows:
myfunc <- function(ds,colname,val1,col1,col2){
# content of new column <colname> should be computed from:
ds[colname] = val1 + ds[col1] * ds[col2] # for each row of ds
return(ds)
}
v1 = 2
data(mtcars)
mt = head(mtcars)
mt
mpg cyl disp hp drat wt qsec vs am gear
carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
apply(mt,'newcol',v1,mt$wt,mt$qsec)
mt
What I would like to see in mt$newcol in first row is: 2 + 2.620 * 16.46 (-> 45.12) and all other rows similiar.
So, how can I send a fixed value (v1) and two values from each row to my function and store returned value in this row in a new column?
Thanks
r
1
Why don't you simply saymtcars$newcol <- 2 + mtcars$weight * mtcars$qsec
?
– vaettchen
Nov 10 at 14:31
@vaettchen: This is just an example. I have a function that gets more information from a server depending on content of cells in each row.
– Tom
Nov 10 at 16:40
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm trying to calculate a new column with a user defined function that needs data from same row and a fixed value valid for all rows:
myfunc <- function(ds,colname,val1,col1,col2){
# content of new column <colname> should be computed from:
ds[colname] = val1 + ds[col1] * ds[col2] # for each row of ds
return(ds)
}
v1 = 2
data(mtcars)
mt = head(mtcars)
mt
mpg cyl disp hp drat wt qsec vs am gear
carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
apply(mt,'newcol',v1,mt$wt,mt$qsec)
mt
What I would like to see in mt$newcol in first row is: 2 + 2.620 * 16.46 (-> 45.12) and all other rows similiar.
So, how can I send a fixed value (v1) and two values from each row to my function and store returned value in this row in a new column?
Thanks
r
I'm trying to calculate a new column with a user defined function that needs data from same row and a fixed value valid for all rows:
myfunc <- function(ds,colname,val1,col1,col2){
# content of new column <colname> should be computed from:
ds[colname] = val1 + ds[col1] * ds[col2] # for each row of ds
return(ds)
}
v1 = 2
data(mtcars)
mt = head(mtcars)
mt
mpg cyl disp hp drat wt qsec vs am gear
carb
Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1
apply(mt,'newcol',v1,mt$wt,mt$qsec)
mt
What I would like to see in mt$newcol in first row is: 2 + 2.620 * 16.46 (-> 45.12) and all other rows similiar.
So, how can I send a fixed value (v1) and two values from each row to my function and store returned value in this row in a new column?
Thanks
r
r
asked Nov 10 at 13:32
Tom
102
102
1
Why don't you simply saymtcars$newcol <- 2 + mtcars$weight * mtcars$qsec
?
– vaettchen
Nov 10 at 14:31
@vaettchen: This is just an example. I have a function that gets more information from a server depending on content of cells in each row.
– Tom
Nov 10 at 16:40
add a comment |
1
Why don't you simply saymtcars$newcol <- 2 + mtcars$weight * mtcars$qsec
?
– vaettchen
Nov 10 at 14:31
@vaettchen: This is just an example. I have a function that gets more information from a server depending on content of cells in each row.
– Tom
Nov 10 at 16:40
1
1
Why don't you simply say
mtcars$newcol <- 2 + mtcars$weight * mtcars$qsec
?– vaettchen
Nov 10 at 14:31
Why don't you simply say
mtcars$newcol <- 2 + mtcars$weight * mtcars$qsec
?– vaettchen
Nov 10 at 14:31
@vaettchen: This is just an example. I have a function that gets more information from a server depending on content of cells in each row.
– Tom
Nov 10 at 16:40
@vaettchen: This is just an example. I have a function that gets more information from a server depending on content of cells in each row.
– Tom
Nov 10 at 16:40
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
dplyr approach:
library(dplyr)
data(mtcars)
myfunc <- function(ds, new_column, val1, col1, col2){
name <- rownames(ds)
ds <- ds %>%
mutate(!!as.name(new_column) := val1 + !!as.name(col1) + !!as.name(col2),
car_name = name) %>%
select(car_name, mpg:!!as.name(new_column))
return(ds)
}
head(
myfunc(ds = mtcars,
new_column = "new_column",
val1 = 2,
col1 = "hp",
col2 = "vs")
)
output
car_name mpg cyl disp hp drat wt qsec vs am gear carb new_column
1 Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 112
2 Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 112
3 Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 96
4 Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 113
5 Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 177
6 Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 108
Great, it almost works. I changed your function to ... := val1 + ... because val1 is not a column but a fixed value. But why do we loose our first column (car names)?
– Tom
Nov 10 at 14:21
@Tom car names are not column. It isrownames()
, and tidy data structure does not treat it.dplyr
would erase it. If you want it, there is a function likerownames_to_column()
– Blended
Nov 10 at 14:45
@Tom , I updated the code. Now the car names in a separate column. HTH
– Aleksandr
Nov 10 at 15:57
1
Thanks, works as expected. I think I should learn more about dplyr.
– Tom
Nov 10 at 16:41
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
dplyr approach:
library(dplyr)
data(mtcars)
myfunc <- function(ds, new_column, val1, col1, col2){
name <- rownames(ds)
ds <- ds %>%
mutate(!!as.name(new_column) := val1 + !!as.name(col1) + !!as.name(col2),
car_name = name) %>%
select(car_name, mpg:!!as.name(new_column))
return(ds)
}
head(
myfunc(ds = mtcars,
new_column = "new_column",
val1 = 2,
col1 = "hp",
col2 = "vs")
)
output
car_name mpg cyl disp hp drat wt qsec vs am gear carb new_column
1 Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 112
2 Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 112
3 Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 96
4 Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 113
5 Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 177
6 Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 108
Great, it almost works. I changed your function to ... := val1 + ... because val1 is not a column but a fixed value. But why do we loose our first column (car names)?
– Tom
Nov 10 at 14:21
@Tom car names are not column. It isrownames()
, and tidy data structure does not treat it.dplyr
would erase it. If you want it, there is a function likerownames_to_column()
– Blended
Nov 10 at 14:45
@Tom , I updated the code. Now the car names in a separate column. HTH
– Aleksandr
Nov 10 at 15:57
1
Thanks, works as expected. I think I should learn more about dplyr.
– Tom
Nov 10 at 16:41
add a comment |
up vote
1
down vote
accepted
dplyr approach:
library(dplyr)
data(mtcars)
myfunc <- function(ds, new_column, val1, col1, col2){
name <- rownames(ds)
ds <- ds %>%
mutate(!!as.name(new_column) := val1 + !!as.name(col1) + !!as.name(col2),
car_name = name) %>%
select(car_name, mpg:!!as.name(new_column))
return(ds)
}
head(
myfunc(ds = mtcars,
new_column = "new_column",
val1 = 2,
col1 = "hp",
col2 = "vs")
)
output
car_name mpg cyl disp hp drat wt qsec vs am gear carb new_column
1 Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 112
2 Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 112
3 Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 96
4 Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 113
5 Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 177
6 Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 108
Great, it almost works. I changed your function to ... := val1 + ... because val1 is not a column but a fixed value. But why do we loose our first column (car names)?
– Tom
Nov 10 at 14:21
@Tom car names are not column. It isrownames()
, and tidy data structure does not treat it.dplyr
would erase it. If you want it, there is a function likerownames_to_column()
– Blended
Nov 10 at 14:45
@Tom , I updated the code. Now the car names in a separate column. HTH
– Aleksandr
Nov 10 at 15:57
1
Thanks, works as expected. I think I should learn more about dplyr.
– Tom
Nov 10 at 16:41
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
dplyr approach:
library(dplyr)
data(mtcars)
myfunc <- function(ds, new_column, val1, col1, col2){
name <- rownames(ds)
ds <- ds %>%
mutate(!!as.name(new_column) := val1 + !!as.name(col1) + !!as.name(col2),
car_name = name) %>%
select(car_name, mpg:!!as.name(new_column))
return(ds)
}
head(
myfunc(ds = mtcars,
new_column = "new_column",
val1 = 2,
col1 = "hp",
col2 = "vs")
)
output
car_name mpg cyl disp hp drat wt qsec vs am gear carb new_column
1 Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 112
2 Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 112
3 Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 96
4 Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 113
5 Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 177
6 Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 108
dplyr approach:
library(dplyr)
data(mtcars)
myfunc <- function(ds, new_column, val1, col1, col2){
name <- rownames(ds)
ds <- ds %>%
mutate(!!as.name(new_column) := val1 + !!as.name(col1) + !!as.name(col2),
car_name = name) %>%
select(car_name, mpg:!!as.name(new_column))
return(ds)
}
head(
myfunc(ds = mtcars,
new_column = "new_column",
val1 = 2,
col1 = "hp",
col2 = "vs")
)
output
car_name mpg cyl disp hp drat wt qsec vs am gear carb new_column
1 Mazda RX4 21.0 6 160 110 3.90 2.620 16.46 0 1 4 4 112
2 Mazda RX4 Wag 21.0 6 160 110 3.90 2.875 17.02 0 1 4 4 112
3 Datsun 710 22.8 4 108 93 3.85 2.320 18.61 1 1 4 1 96
4 Hornet 4 Drive 21.4 6 258 110 3.08 3.215 19.44 1 0 3 1 113
5 Hornet Sportabout 18.7 8 360 175 3.15 3.440 17.02 0 0 3 2 177
6 Valiant 18.1 6 225 105 2.76 3.460 20.22 1 0 3 1 108
edited Nov 10 at 15:56
answered Nov 10 at 13:43
Aleksandr
1,361716
1,361716
Great, it almost works. I changed your function to ... := val1 + ... because val1 is not a column but a fixed value. But why do we loose our first column (car names)?
– Tom
Nov 10 at 14:21
@Tom car names are not column. It isrownames()
, and tidy data structure does not treat it.dplyr
would erase it. If you want it, there is a function likerownames_to_column()
– Blended
Nov 10 at 14:45
@Tom , I updated the code. Now the car names in a separate column. HTH
– Aleksandr
Nov 10 at 15:57
1
Thanks, works as expected. I think I should learn more about dplyr.
– Tom
Nov 10 at 16:41
add a comment |
Great, it almost works. I changed your function to ... := val1 + ... because val1 is not a column but a fixed value. But why do we loose our first column (car names)?
– Tom
Nov 10 at 14:21
@Tom car names are not column. It isrownames()
, and tidy data structure does not treat it.dplyr
would erase it. If you want it, there is a function likerownames_to_column()
– Blended
Nov 10 at 14:45
@Tom , I updated the code. Now the car names in a separate column. HTH
– Aleksandr
Nov 10 at 15:57
1
Thanks, works as expected. I think I should learn more about dplyr.
– Tom
Nov 10 at 16:41
Great, it almost works. I changed your function to ... := val1 + ... because val1 is not a column but a fixed value. But why do we loose our first column (car names)?
– Tom
Nov 10 at 14:21
Great, it almost works. I changed your function to ... := val1 + ... because val1 is not a column but a fixed value. But why do we loose our first column (car names)?
– Tom
Nov 10 at 14:21
@Tom car names are not column. It is
rownames()
, and tidy data structure does not treat it. dplyr
would erase it. If you want it, there is a function like rownames_to_column()
– Blended
Nov 10 at 14:45
@Tom car names are not column. It is
rownames()
, and tidy data structure does not treat it. dplyr
would erase it. If you want it, there is a function like rownames_to_column()
– Blended
Nov 10 at 14:45
@Tom , I updated the code. Now the car names in a separate column. HTH
– Aleksandr
Nov 10 at 15:57
@Tom , I updated the code. Now the car names in a separate column. HTH
– Aleksandr
Nov 10 at 15:57
1
1
Thanks, works as expected. I think I should learn more about dplyr.
– Tom
Nov 10 at 16:41
Thanks, works as expected. I think I should learn more about dplyr.
– Tom
Nov 10 at 16:41
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53239473%2fcalculate-new-column-in-dataframe-or-list-using-a-function-with-column-as-param%23new-answer', 'question_page');
}
);
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
1
Why don't you simply say
mtcars$newcol <- 2 + mtcars$weight * mtcars$qsec
?– vaettchen
Nov 10 at 14:31
@vaettchen: This is just an example. I have a function that gets more information from a server depending on content of cells in each row.
– Tom
Nov 10 at 16:40