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










share|improve this question


















  • 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















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










share|improve this question


















  • 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













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










share|improve this question













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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 10 at 13:32









Tom

102




102








  • 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














  • 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








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












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





share|improve this answer























  • 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 , 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











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














 

draft saved


draft discarded


















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
































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





share|improve this answer























  • 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 , 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















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





share|improve this answer























  • 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 , 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













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





share|improve this answer














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






share|improve this answer














share|improve this answer



share|improve this answer








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 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






  • 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












  • @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






  • 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


















 

draft saved


draft discarded



















































 


draft saved


draft discarded














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




















































































Popular posts from this blog

Full-time equivalent

Bicuculline

さくらももこ