I am trying to run a panel regression in R studio. When I use cbind command
x<- cbind(DEX, GRW , Debt, Life)
for my independent variables,it returns this error;
" Error in cbind(DEX, GRW, Debt, Life) : object 'DEX' not found"
However my dependent variable works fine with cbind
as shown below
y<- cbind(GDP)
Can you help?
Thanks.
You defined one and only one object when you executed:
tino=read.delim("clipboard")
The column names of that object are not handled as other objects. If you wnated to create a new object from that dataframe you could do this:
x <- with(tino, cbind(DEX, GRW , Debt, Life) )
It's possible this might do violence to the contents of x and it would be safer to extract as just those columns of hte dataframe, tino:
x <- tino[ , c('DEX', 'GRW' , 'Debt', 'Life')]
You should realize that vectors passed to cbind will get turned into matrices (where all their elements have the same class and no other attributes are supported). Matrices have different features than dataframes (which can have multiple column class attributes).
Related
In my data frame I am trying to sort the data in descending order. I am using the below line of code for sorting my data and it works as intended.
CNS25VOL <- CNS25VOL[order(-CNS25VOL$MATVOL22), ]
However if I refer to the same column by it's index number, the code throws an error
CNS25VOL <- CNS25VOL[order(-CNS25VOL[, 2]), ]
Error thrown is
Error in CNS25VOL[, 2] : incorrect number of dimensions
While I do have a solution to what I am intending to do, but issue I see is if all of a sudden name of my column changes the code won't work. I know that their position will stay same in the data frame.
How can we handle it.
order(-CNS25VOL[, 2]) order here does expect a vector which you try to construct via the [] in CNS25VOL[, 2]. Normal dataframes will return a vector consisting only of the 2nd column. A tibble however will return a tibble with only one column.
You can reproduce the behaviour of normal data.frames with the drop = FALSE argument to [] as in
CNS25VOL[, 2, drop = TRUE]
Try to always be aware whether you are using a standard data.frame or a tibble or a data.table because they look very similar and are not in the details. Also see https://tibble.tidyverse.org/reference/subsetting.html
dplyr functions tend to give you a tibble back even if you fed them a classical data.frame.
I'm having issues with a specific problem I have a dataset of a ton of matrices that all have V1 as their column names, essentially NULL. I'm trying to write a loop to replace all of these with column names from a list but I'm running into some issues.
To break this down to the most simple form, this code isn't functioning as I'd expect it to.
nameofmatrix <- paste('column_', i, sep = "")
colnames(eval(as.name(nameofmatrix))) <- c("test")
I would expect this to take the value of column_1 for example, and replace (in the 2nd line) with "test" as the column name.
I tried to break this down smaller, for example, if I run print(eval(as.name(nameofmatrix)) I get the object's column/rows printed as expected and if I run print(colnames(eval(as.name(nameofmatrix))) I'm getting NULL as expected for the column header (since it was set as V1).
I've even tried to manually type in the column name, such as colnames(column_1) <- c("test) and this successfully works to rename the column. But once this variable is put in the text's place as shown above, it does not work the same. I'm having difficulties finding a solution on how to rename several matrix columns after they have been created with this method. Does anyone have any advice or suggestions?
Note, the error I'm receiving on trying to run this is
Error in eval([as.name](nameofmatrix)) <- \`vtmp\` : could not find function "eval<-"
We could return the values of the objects in a list with get (if there are multiple objects use mget, then rename the objects in the list and update those objects in the global env with list2env
list2env(lapply(mget(nameofmatrix), function(x) {colnames(x) <- newnames
x}), .GlobalEnv)
It can also be done with assign
data(mtcars)
nameofobject <- 'mtcars'
assign(nameofobject, `colnames<-`(get(nameofobject),
c('mpg1', names(mtcars)[-1])))
Now, check the names of 'mtcars'
names(mtcars)[1]
#[1] "mpg1"
Sometimes I have code which references a specific dataset based on some variable ID. I have then been creating lines of code using paste0, and then eval(parse(...)) that line to execute the code. This seems to be getting sloppy as the length of the code increases. Are there any cleaner ways to have dynamic data reference?
Example:
dataset <- "dataRef"
execute <- paste0("data.frame(", dataset, "$column1, ", dataset, "$column2)")
eval(parse(execute))
But now imagine a scenario where dataRef would be called for 1000 lines of code, and sometimes needs to be changed to dataRef2 or dataRefX.
Combining the comments of Jack Maney and G.Grothendieck:
It is better to store your data frames that you want to access by a variable in a list. The list can be created from a vector of names using get:
mynames <- c('dataRef','dataRef2','dataRefX')
# or mynames <- paste0( 'dataRef', 1:10 )
mydfs <- lapply( mynames, get )
Then your example becomes:
dataset <- 'dataRef'
mydfs[[dataset]][,c('column1','column2')]
Or you can process them all at once using lapply, sapply, or a loop:
mydfs2 <- lapply( mydfs, function(x) x[,c('column1','column2')] )
#G.Grothendieck has shown you how to use get and [ to elevate a character value and return the value of a named object and then reference named elements within that object. I don't know what your code was intended to accomplish since the result of executing htat code would be to deliver values to the console, but they would not have been assigned to a name and would have been garbage collected. If you wanted to use three character values: objname, colname1 and colname2 and those columns equal to an object named after a fourth character value.
newname <- "newdf"
assign( newname, get(dataset)[ c(colname1, colname2) ]
The lesson to learn is assign and get are capable of taking character character values and and accessing or creating named objects which can be either data objects or functions. Carl_Witthoft mentions do.call which can construct function calls from character values.
do.call("data.frame", setNames(list( dfrm$x, dfrm$y), c('x2','y2') )
do.call("mean", dfrm[1])
# second argument must be a list of arguments to `mean`
i'm writing a script that reads two .txt file in two vectors. After that I want to make a Spearman's rank correlation and plot the result.
The first vectors value's length is 12-13 characters (e.g. 7.3445555667 or 10.3445555667) and the second vectors value's length is one character (e.g. 1 or 2).
The code:
vector1 <- read.table ("D:...path.../mytext1.txt", header=FALSE)
vector2 <- read.table ("D:...path.../mytext2.txt", header=FALSE)
cor.coeff = cor(vector1 , vector2 , method = "spearman")
cor.test(vector1 , vector2 , method = "spearman")
plot(vector1.var, vector2.var)
The .txt files contain only numeric values.
I'm getting two errors, the first in line 4 it's like " 'x' have to be a numeric vector"
and the second error occurs in line 5 it's like "object vector 1.var couldn't be found"
I also tried
plot(vector1, vector2)
instead of
plot(vector1.var, vector2.var)
But then there's an error like "Error in stripchart.default (x1,...) : invalid plot-method
The implementation is orientated at http://www.gardenersown.co.uk/Education/Lectures/R/correl.htm#correlation
I doubt vector1 and vector2 are vectors. Reading ?read.table we note in the Value section:
Value:
A data frame (‘data.frame’) containing a representation of the
data in the file.
....
So even if your two text files contain just a single variable, the two objects read in will be data frames with a single component each.
Secondly, your data files don't contain headers so R will make up a variable name. I haven't tested this but IIRC your the variables in vector1 and vector2 will both be called X1. Do head(vector1) and the same on vector2 (or names(vector1)) to see how your objects look in R.
I can see why you might think vector1.var might work, but you should realise that as far as R was concerned it was looking for an object named vector1.var. The . is just any other character in R object names. If you meant to use . as a subsetting or selection operator, then you need to read up on subsetting operators in R. These are $ and [ and [[. See for example the R Language Definition manual or the R manual.
I suspect you could just change your code to:
vector1 <- read.table ("D:...path.../mytext1.txt", header=FALSE)[, 1]
vector2 <- read.table ("D:...path.../mytext2.txt", header=FALSE)[, 1]
cor.coeff <- cor(vector1 , vector2 , method = "spearman")
cor.test(vector1 , vector2 , method = "spearman")
plot(vector1, vector2)
But I am supposing quite a bit about what is in your two text files...
str is a very useful function (see ?str for more) that one should use often, especially to verify R object types. A quick str(vector1) and str(vector2) will tell you if those columns were read as characters instead of numeric. If so, then use as.numeric(vector1) to typecast the data in each vector.
Also, names(vector1) and names(vector2) will tell you what the column names are and likely resolve your plotting issue.
I am trying to initiate this code using the zoo command:
gld <- zoo(gld[,7], gld_dates)
Unfortunately I get an error message telling me this:
Error in `[.data.frame`(gld, , 7) : undefined columns selected
I want to use the zoo function to create zoo objects from my data.
The function should take two arguments: a vector of data and
a vector of dates.
This is the data I am using[LINK BROKEN].
I believe I have have 7 columns in my data set. Any ideas?
The code I am trying to implement is found here[LINK BROKEN].
Is their anything wrong with this code?
You don't say what your gld_dates is exactly, but if gld starts as your original data and you want to make a zoo object of the 7th column ordering by the 1st column (dates), I can do
gld_zoo <- zoo(gld[, 7], gld[, 1])
just fine. Equivalently, but with more readability,
gld_zoo <- zoo(gld$Adj.close, gld$Date)
reminds me what each column is.
Subsetting requires the names of the subset columns to match those in the data frame. This code subsets the dataset french_fries with potat instead of potato:
data("french_fries")
df_potato <- french_fries[, c("potatoes")]
and it fails with:
Error in `[.data.frame`(french_fries, , c("potatoes")) :
undefined columns selected
but using the right name potato works:
df_potato <- french_fries[, c("potato")]