0

How to make a matrix P containing the proportions p1 to ps for s variables from the initial dataframe (which contains columns p1 to ps)

This is an R problem. I have a dataframe that includes variables p1 to ps as well as other variables. I want to transfer the values for variables p1 to ps from the dataframe to a matrix P for use in other routines. I can readily do this when I know the number of columns s (s = 5 in the example supplied below) using the code below (test data is in dataframe ALL_test for a five column example).

The following code reads in the example dataframe ALL_test.

ALL_test <- data.frame(
x = c(50,75,45), p1 = c(1, 0, 0), p2 = c(0, .4, .1),  p3 = c(0, .2, .3),
p4 = c(0, .4, .1),  p5 = c(0, 0, .5)   
)
P <- with(ALL_test, cbind(p1, p2, p3, p4, p5))
colnames(P)<- c("p1","p2","p3","p4","p5")

Outputting P shows that this solution works when based on the known value 5 of s, the number of columns I wish to transfer to a matrix P.

I want to develop code where I supply ā€˜s’ that will return s columns in the matrix P. The code that was kindly supplied in the first response to this post gives me a list that contains the names p1 to ps but I do not see how to use this to extract the columns
p1 to p5 from the dataframe.

I know that this is probably trivial but I cannot sort it.

I have tried (all of which just gives strings of p1 to ps)

s <- 5
nam1 <- paste("p", 1:s, sep = "", collapse = ", ")      
nam1   # this returns "p1, p2, p3, p4, p5"
cat(nam1, "\n") # returns p1, p2, p3, p4, p5 but this does not work in 
P <- with(ALL, cbind(cat(nam1, "\n")))
johnc
  • 1
  • 2
  • Please read [this](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) and also please keep in mind that when posting questions on SO you need to add the language you are using tag. – David Arenburg Jan 27 '16 at 13:59

1 Answers1

1

I think I see what you're trying to do... but everything you've tried just creates one string of s labels, rather than a list of length s.

How about: with(ALL, paste("p",as.character(seq(1,s)),sep=""))

Edit

With the updated question you've essentially got a data frame that you want to take a subset of columns for and create a matrix out of, so that's how I'd go about building an expression to do it (someone feel free to tell me a better way of doing this!)

Subset the data frame (using the code I posted before as a vector for an %in test):

temp<-ALL_test[,colnames(ALL_test)%in%paste("p",as.character(seq(1,s)),sep="")]

Then create a matrix from that:

P <- data.matrix(temp)

Naturally there's nothing stopping you combining all of that into:

P <-data.matrix(ALL_test[,colnames(ALL_test)%in%paste("p",as.character(seq(1,s)),sep="")])

halfer
  • 19,824
  • 17
  • 99
  • 186
Tim Hirst
  • 461
  • 3
  • 15