Creating a variable using string names in R

sak

New Member
#1
I want to create a variable name from string names. E.g.

The Array has columns named as price, temperature, pressure.

Vector <- function(Array,string)
{
Vector = Array$string
}

So, when I make the following call:
v1 = Vector(giantArray,'temperature') #extracts temperature column from giantArray
v2 = Vector(giantArray,'pressure') #extracts pressure column from giantArray
 

trinker

ggplot2orBust
#2
I'll preface this by saying I work exclusively with dataframes not arrays. I'm applying what I know about dataframes to an array. It may or may not work.

You could try to do the following:

Code:
array.name[,c("temperature")]
or

Code:
subset(array.name,select=temperature)
I don't know if this handles your problem. If you want it as a function:

Code:
Vector <- function(Array,string){
subset(Array,select=c(string))
}
...This all assumes you have a 2 dimensional array.

This stuff works on data.frames and I assume it works on arrays as well. If not maybe you can convert to a dataframe and then convert back to an array.
 
Last edited:

sak

New Member
#3
The method you suggested here retains the name i.e it stays as data. I am looking for Vector to have no variable name and just the numbers in rows. i.e. how do I create the numeric array. Ultimately I am going to use the vectors in regression so I need a real vector.
 
Last edited:

trinker

ggplot2orBust
#4
Code:
d<-c(1:40)
arr<-array(d,c(4,10))
colnames(arr)<-c("A","B","C","D","E","F","G","H","I","J")
arr
arr[,"B"]
sc<-arr[,"B"]
sc
is.vector(sc)
 

trinker

ggplot2orBust
#5
Ultimately I am going to use the vectors in regression so I need a real vector.
Why do you need to tear the array appart to run regression?

Code:
#convert the array to a data frame:
arr2<-as.data.frame(arr)
mod<-lm(A~B+C+D+J,data=arr2)
summary(mod)
This output is terrible because the data perfectly correlates but you get the idea.