Reputation: 233
I have a very simple line of code I would like to run. I am creating this vector:
x1<- rep("T", 576)
However, I derived the number 576 from this line of code:
dim(x)
(x is a dataframe which is 576 rows long)
I need to create vectors similar to x1 for many other other data frames which are all of varying dimensions. I do not want to have to manually type the dimension value into the rep formula every time. Is there a way to combine these two formulas into one?
Upvotes: 0
Views: 49
Reputation: 1
Have you considered making a list of data frames and then writing a loop to calculate a new vector for each data frame? The below code creates two data frames of varying size, throws them in a list, and then uses a for loop to get the dimensions of each one and assigns that to a vector (obviously replace "z in 1:2" with however many data frames you have). Hope that helps.
x1<-data.frame(1:3,4:6)
x2<-data.frame(7:10,11:14)
x<-list(x1,x2)
z<-0
for (z in 1:2) {
t<-as.data.frame(lapply(x[z], dim))
t[1,1]
d<-rep("T",t[1,1])
assign(paste("vector",z,sep=""),d)
}
Upvotes: 0
Reputation: 604
Try dim(x)[1]
or nrow(x)
to extract the dimension and feed into your call:
x1 <- rep("T", nrow(x1))
Upvotes: 1