add value to multiple columns in R
up vote
0
down vote
favorite
I want to update my dataframe adding value in various columns.
My dataframe has A-Z columns name.
I created a list specifiying which column want to update.
upd<-c("a","b","c","d")
And i used simple R syntax to update
df<-df[,names(df)%in%upd]+5
But I only have a,b,c,d columns.
is there one way to save all columns?.
thanks
r
|
show 1 more comment
up vote
0
down vote
favorite
I want to update my dataframe adding value in various columns.
My dataframe has A-Z columns name.
I created a list specifiying which column want to update.
upd<-c("a","b","c","d")
And i used simple R syntax to update
df<-df[,names(df)%in%upd]+5
But I only have a,b,c,d columns.
is there one way to save all columns?.
thanks
r
Trydf[,names(df) %in% upd] <- df[,names(df) %in% upd] + 5
– markus
Nov 7 at 12:30
@markus thanks mate it works! is this way efficient?
– liguang
Nov 7 at 12:32
That is pretty straight forward, no?
– markus
Nov 7 at 12:35
@markus yes thanks!
– liguang
Nov 7 at 12:36
1
or for readability, but slower :library(dplyr); df <- mutate_at(df, upd, ~.+5)
– Moody_Mudskipper
Nov 7 at 12:43
|
show 1 more comment
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I want to update my dataframe adding value in various columns.
My dataframe has A-Z columns name.
I created a list specifiying which column want to update.
upd<-c("a","b","c","d")
And i used simple R syntax to update
df<-df[,names(df)%in%upd]+5
But I only have a,b,c,d columns.
is there one way to save all columns?.
thanks
r
I want to update my dataframe adding value in various columns.
My dataframe has A-Z columns name.
I created a list specifiying which column want to update.
upd<-c("a","b","c","d")
And i used simple R syntax to update
df<-df[,names(df)%in%upd]+5
But I only have a,b,c,d columns.
is there one way to save all columns?.
thanks
r
r
edited Nov 7 at 12:30
asked Nov 7 at 12:23
liguang
96
96
Trydf[,names(df) %in% upd] <- df[,names(df) %in% upd] + 5
– markus
Nov 7 at 12:30
@markus thanks mate it works! is this way efficient?
– liguang
Nov 7 at 12:32
That is pretty straight forward, no?
– markus
Nov 7 at 12:35
@markus yes thanks!
– liguang
Nov 7 at 12:36
1
or for readability, but slower :library(dplyr); df <- mutate_at(df, upd, ~.+5)
– Moody_Mudskipper
Nov 7 at 12:43
|
show 1 more comment
Trydf[,names(df) %in% upd] <- df[,names(df) %in% upd] + 5
– markus
Nov 7 at 12:30
@markus thanks mate it works! is this way efficient?
– liguang
Nov 7 at 12:32
That is pretty straight forward, no?
– markus
Nov 7 at 12:35
@markus yes thanks!
– liguang
Nov 7 at 12:36
1
or for readability, but slower :library(dplyr); df <- mutate_at(df, upd, ~.+5)
– Moody_Mudskipper
Nov 7 at 12:43
Try
df[,names(df) %in% upd] <- df[,names(df) %in% upd] + 5
– markus
Nov 7 at 12:30
Try
df[,names(df) %in% upd] <- df[,names(df) %in% upd] + 5
– markus
Nov 7 at 12:30
@markus thanks mate it works! is this way efficient?
– liguang
Nov 7 at 12:32
@markus thanks mate it works! is this way efficient?
– liguang
Nov 7 at 12:32
That is pretty straight forward, no?
– markus
Nov 7 at 12:35
That is pretty straight forward, no?
– markus
Nov 7 at 12:35
@markus yes thanks!
– liguang
Nov 7 at 12:36
@markus yes thanks!
– liguang
Nov 7 at 12:36
1
1
or for readability, but slower :
library(dplyr); df <- mutate_at(df, upd, ~.+5)
– Moody_Mudskipper
Nov 7 at 12:43
or for readability, but slower :
library(dplyr); df <- mutate_at(df, upd, ~.+5)
– Moody_Mudskipper
Nov 7 at 12:43
|
show 1 more comment
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53189411%2fadd-value-to-multiple-columns-in-r%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Try
df[,names(df) %in% upd] <- df[,names(df) %in% upd] + 5
– markus
Nov 7 at 12:30
@markus thanks mate it works! is this way efficient?
– liguang
Nov 7 at 12:32
That is pretty straight forward, no?
– markus
Nov 7 at 12:35
@markus yes thanks!
– liguang
Nov 7 at 12:36
1
or for readability, but slower :
library(dplyr); df <- mutate_at(df, upd, ~.+5)
– Moody_Mudskipper
Nov 7 at 12:43