This question already has answers here:
Assign multiple new variables on LHS in a single line
(15 answers)
Closed 9 years ago.
In python it is possible to decompose a list
x=[1,2,3]
a,b,c=x # a=1 b=2 c=3
is it possible to do something similar in R?
for example something like:
x=matrix(rnorm(100),10,10)
[u d v]=svd(x) # instead of u=svd$u d=svd$d v=svd$v
Not without some hackery and fiddly global assignment, and my personal opinion is that this is not a good thing to do.
Why? Well, if the results from svd (to use your example) are returned together there's a good reason they are returned together - they belong together. Once you break it up you lose that relationship. The only win is having fewer characters to type, and that's not one of the things we optimise with programming - readability should win over that.
Related
This question already has answers here:
What does the dot mean in R – personal preference, naming convention or more?
(3 answers)
Closed 1 year ago.
I came across a piece of code from https://rpubs.com/boyerag/297592
On one line they have the following code:
ndvi.array <- ncvar_get(nc_data, "NDVI")
I do not understand what is the significance of ndvi.array. Is this some special way of creating an array in R? I assumed arrays had to be created using the array function. Since data in netCDF format and essentially each variable is an array based on the dimensions, is this an elaborate way of naming the identifier or does ndvi have some association with a parameter 'array'
I am new to R, I didn't realize dots don't mean anything. I assumed it was some weird data structure I didn't know. So from this example, it just creates an R variable named "ndvi.array".
This question already has answers here:
How to know a dimension of matrix or vector in R?
(6 answers)
Closed 3 years ago.
I know this is probably a very simple question but I can't seem to find the answer anywhere online. I am trying to print just the number of data points inside of a variable that I created but I can't figure out how.
I tried using summary() or num() or n() but I am really just making stuff up here and cannot seem to figure it out at all.
For my specific example I have a data set on peoples heights, age, weight, gender, stuff like that. I used
one_sd_weight <- cdc$weight[abs(cdc$weight - mean(cdc$weight)) <= sd(cdc$weight)]
to determine how many of the weights fall within one standard deviation of the mean. After I do this, I can see that on the right side it created a new variable called one_sd_weight that contains 14152 out of the original 20000 entries. How do I print the number 14152 as a variable? For the work I am doing I need to create a new variable that just contains one number, 14152 or whatever number is produced when I run the code above. For example, I need to create
n_one_sd <- 14152
without typing in 14152, instead typing some function that grabs the number of entries in one_sd_weight.
I have tried things like summary() and n() but only receive error messages in return. Any help is greatly appreciated!!
n_one_sd <- length(one_sd_weight)
You're looking for length (in case of a vector) or nrow in case of a matrix/data.frame.
Or you can use NROW() for both, that should work too.
This question already has answers here:
Regular expression to enforce complex passwords, matching 3 out of 4 rules
(4 answers)
Closed 8 years ago.
I would like to use a regular expression in the ASP.NET membership. What is a regular express for the below?
at least 8 characters long
include at least one upper case letter
one lower case letter
one number
try this..
^((?=.*\d)(?=.*[A-Z])(?=.*[a-z]).{8,})
You could use something like that:
^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])[a-zA-Z\d=:;<>,~!##\\$%/^&)(\[\]+-]{8,}$
Test it here.
You may also want to learn about the "?=" thing, which is called "positive lookahead" here.
In short, when all three lookaheads (.*\d and .*[a-z] and .*[A-Z]) are matched (and are discarded), the main regex [a-zA-Z\d=:;<>,~!##\\$%/^&)(\[\]+-]{8,} can be matched too.
Do you have to do this in one regex? I would make each of those rules one regex, and test for them individually. I suspect you code will end up being simpler, and you'll save yourself and whoever has to maintain your application several headaches.
This question already has an answer here:
Generally disable dimension dropping for matrices?
(1 answer)
Closed 9 years ago.
The default behavior in R for reducing a 2 dimensional matrix to 1 row is to actually drop a dimension. This can be "fixed" by putting drop=FALSE at the end of the matrix search.
Is there a way to make this the default? I have a pretty long program and just realized I'm going to have to add this in about 100 places if there isn't...
I searched ?options, ?'[', and ?matrix to no avail.
You can redefine `[` like this:
old <- `[`
`[` <- function(...) { old(..., drop=FALSE) }
This modification should be local to the interactive scope and therefore not affect routines which rely on the other behaviour. No guarantees, though. And be prepared that code of this form will be likely to confuse readers of your code, who are used to the other semantics.
Perhaps you can make this change local to a specific function, instead of all your code?
One alternative would be writing your own class for the matrix objects, for which you can provide your own subset operator implementation. This makes sense if you construct matrices in a very limited number of places, but might be a problem if there is a large number of code paths constructing these matrices.
This question already has answers here:
How to drop columns by name in a data frame
(12 answers)
Closed 9 years ago.
An easy one I suppose though my searches have been pretty fruitless --
given
z=data.frame(X.39=rnorm(20),X.40=rnorm(20),X.51=rnorm(20))
the subsetting operation
z[,c('X.39','X.51')]
works. but
z[,-c('X.39','X.51')]
gives me
Error in -c("X.39", "X.51") : invalid argument to unary operator
why is that and how do I remove a set of columns using a list of column names?
EDIT
I know that I can always use
z[,!names(z) %in% c('X.39','X.51')]
but I'm looking for a lazier solution
EDIT2
Most of the discussion has been in the comment section but to close this off for good order, the gist of this is that a lazier solution (direct reference by name) is not possible. This appears to be designed in.
You could use setdiff function, but I can't say if its the most elegant solution:
z[, setdiff(names(z), c('X.39','X.51'))]