How to assign same value to multiple variables in one line?
You might try simply:
for (x in letters) assign(x,1)
How to assign multiple variables values in one line in R?
One option is list2env
by placing it in a named
list
list2env(setNames(as.list(1:4), paste0("beta_", 0:3)), envir = .GlobalEnv)
But, it may be better to work on the list
itself instead of creating multiple objects in the global environment
Assigning multiple variables to multiple values at once R?
You can use setNames
, i.e.
setNames(c('A', 'B', 'C'), paste0('x', seq(3)))
# x1 x2 x3
#"A" "B" "C"
Can you assign two variables simultaneously in R?
No, you can't do that in R (at least, not in base or any packages I'm aware of).
The closest you could come would be to assign objects to different elements of a list. If you really wanted, you could then use list2env
to put the list elements in an environment (e.g., the global environment), or use attach
to make the list elements accessible, but I don't think you gain much from these approaches.
If you want a function to return more than one value, just put them in a list. See also r - Function returning more than one value.
You can assign multiple variables the same value as below. Even here, I think the code is unusual and less clear, I think this outweighs any benefits of brevity. (Though I suppose it makes it crystal clear that all of the variables are the same value... perhaps in the right context it makes sense.)
x <- y <- z <- 1
# the above is equivalent to
x <- 1
y <- 1
z <- 1
Can't assign multiple line variables in R
Pipes might be what you're looking for.
This code should work on the train.csv Data which you're supposedly using.
age_survived <- summarise(group_by(train, Age, Survived), count=n()) %>%
filter(Survived == 1) %>%
rename("n_survived" = "count")
Would like to assign multiple variables from split() on one line of code
It may look like a simple answer (that you may have already considered), but well here it goes: using a single local var to hold the split()
result, then refer to it in fields' assignments ->
Simple answer:
{
local ports = ['tcp-1514', 'tcp-8080', 'tcp-8443'],
ports: [
local name_split = std.split(name, '-');
{
name: name,
protocol: name_split[0],
containerPort: name_split[1],
}
for name in ports
],
}
Obfuscated answer (no interim local w/split() result):
// Return a map from zipping arr0 (keys) and arr1 (values)
local zipArrays(arr0, arr1) = std.foldl(
// Merge each (per-field) object into a single obj
function(x, y) x + y,
// create per-field object, e.g. { name: <name> },
std.mapWithIndex(function(i, x) { [arr0[i]]: x }, arr1),
{},
);
{
local ports = ['tcp-1514', 'tcp-8080', 'tcp-8443'],
// Carefully ordered set of fields to "match" against: [name] + std.split(...)
local vars = ['name', 'protocol', 'containerPort'],
ports: [
zipArrays(vars, [name] + std.split(name, '-'))
for name in ports
],
}
Lua: Usage rules for the single line multiple variable declaration
local a,b=
33 --a=33 b=nil
for i=1,2 do
a+=i --a=34,b=nil --a=3,b=34
a,b=i,a --a=1,b=34 --a=2,b=3
end
is equivalent to
local a, b = 33, nil
for i=1,2 do
a+=i
a,b=i,a
end
This is syntactically correct. Hence there is no error message.
local a,b=
33 --a=33 b=nil
for i=1,2 do
a+=i --a=34,b=nil --a=3,b=34
a,b=i,a --a=1,b=34 --a=2,b=3
end,33
Is equivalent to
local a,b= 33, nil
for i=1,2 do
a+=i
a,b=i,a
end
,33
You cannot have ,33
isolated in your code. That doesn't make sense. Hence the error.
As Paul already explained, you can only assign a comma separated list of expressions. A for loop is not an expression, it's a statement. It should be obvious that you cannot have a statement in the middle of a comma separated list of expressions.
How can I simultaneously assign value to multiple new columns with R and dplyr?
I think the best you are going to do is a do()
to modify the data.frame. Perhaps
base %>% do(cbind(., setNames(as.list(f()), c("b","c","d"))))
would probably be best if f()
returned a list in the first place for the different columns.
How to assign multiple values as once in R?
You can use the assign
to assign a value to variable. The variable names are strings.
In the code below I've looped though the elements of the list and assigned names from the vector names
.
l <- split(mtcars, mtcars$cyl)
names <- c('cyl4', 'cyl6', 'cyl8')
for (i in 1:length(names)) assign(names[i], l[[i]])
Or the list2env
function as pointed out by @Roland (and others). The attach
function does something similar.
l <- split(mtcars, mtcars$cyl)
names(l) <- c('cyl4', 'cyl6', 'cyl8')
list2env(l, envir = .GlobalEnv)
Or
attach(l)
Although, as others have pointed out. This might not be the best idea; it's easier to keep track of things inside lists. You can just access them as need be with $
.
l$cyl4
Related Topics
Pasting Two Vectors With Combinations of All Vectors' Elements
Chopping a String into a Vector of Fixed Width Character Elements
How to Change Multiple Date Formats in Same Column
Create Sequence of Repeated Values, in Sequence
Add Correct Century to Dates With Year Provided as "Year Without Century", %Y
Aggregate a Data Frame Based on Unordered Pairs of Columns
Dplyr Mutate/Replace Several Columns on a Subset of Rows
Check Existence of Directory and Create If Doesn't Exist
Add a Variable to a Data Frame Containing Max Value of Each Row
How to Use Grep()/Gsub() to Find Exact Match
Convert Column With Pipe Delimited Data into Dummy Variables
Reshape Multiple Values At Once
Increment by 1 For Every Change in Column