Functionally creating variables using string names in R -
The same argument I am trying to generate a function to create a bunch of columns on a data frame containing the same naming conventions Unfortunately, I bump into some strange behavior while creating the variable, and I hope someone can tell what's going on.
df < - data.frame (var1 = c (1,2,3), var2 = c (3,4,5), var3 = c ("foo", "bar", "baz")) doesnotwork & lt; - function (df, varname) {df [paste (varname, "_square", sep = ""]]
dfBad
in var1
Instead of a variable called a variable var1
and a variable named var1_squared
as I had hoped.
The function below All values of the original variables around this value, according to the name of the new variable, then only the new variable will have the same operation , But it is unpleasant in a way, and I am not sure what will happen if I need to use logic in many variables.
Works <- Function (df, varname) {df [paste (varname, "_square", sep = [2] (Previous) [2] (Previous) [2] (2) (2) Df)} dfGood <- Works ( Df, "var1") dfGood var1 var2 var3 var1_square 1 1 3 foo 1 2 2 4 times 4 3 3 5 baz 9
Here no guidance is highly appreciated, especially if the variable Swings between names of strings and column objects for references A good way of doing this.
You are missing a comma.
df < - dat A.frame (var1 = c (1,2,3), var2 = c (3,4,5), var3 = c ("foo", "bar", "falcon")) - Function (df, varnama) {df [, paste (varna, "_square", sep = ""]]
Edit: Okay then the above answer works, but this is actually the question of Does not answer why the other works.
For example:
Multiplication Works & lt; - Function (df, varname) {df [paste (varname, "_square", sep = "")] & lt; - Df [varname] * 2 return (df)} As
As all other non-exponential operators do, if we look at the data. Frame Operator source code, See the bit:
Ops.data.frame ... if (. "%" ("+", "-", "*", "/", "%%" , "% /%")) {Name (value) & lt; - cn data.frame (value, line.name = rn, check.name = FALSE, check.rows = FALSE)} other matrix (list, value, recursive = FALSE, use.names = FALSE), nrow = nr, dimnames = List (rn, cn)) ...
Actually it is saying that if the operator is one of them, then a data with the given names. Return the frame, otherwise return a matrix with the given names. For some reason, "^" operator is not the only one listed, we can very easily confirm it:
df < - data.frame (var1 = c (1,2,3), var2 = c (3,4,5), var3 = c ("foo", "bar", "falcon")) square (df ["var1" "] * 2)> [1]" data.frame "category (df [" var1 "] ^ 2) & gt; [1] With "matrix" exponentonone, and only exponentiation, the dimnames of the matrix reverse the new column names of your data. Frame when you assign it R is strange, comic, that means that you can also get your code to work by as.data.frame ()
wrap around If you use actually using your initial function strange:
❥ name (dfBad) [1] "var1" "var2" "Var3" "var1_square" ❥ dfBad var1 var2 var3 var1 var1 1 1 3 Foo 1 2 2 4 times 4 3 3 5 buz 9 ❥ str (dfBad) 'data.frame': 3 object 4 variables: $ var1: num 1 2 3 $ var2: num 3 4 5 $ var3: factor w / 3 level "bar", "falcon", "foo": 3 1 2 $ var1_square: num [1: 3, 1] 1 4 9 .. - attr (*, "Dimnames") = 2's list .. .. $: zero .. .. $: chr "var1"
R < knows The column is the correct name, but you show the name of the matrix you have left in it.
Comments
Post a Comment