Converting each item of a character vector into column headers without looping

INPUT

  1. specimens: character vector of 60 items: specimen1A, specimen1B, specimen2A ... specimen 30B.

DESIRED OUTPUT

  1. A matrix or a dataframe in which each item in specimens is the name of a column in the matrix/dataframe.
  2. The number of rows must be set to a fixed value (any).
  3. The data for the cells will be filled with subsequent code so can be left as NA.

For example:

   specimen1A  specimen1B specimen2A ... specimen 30B
1   NA          NA              NA         NA
2   NA          NA              NA         NA
3   NA          NA              NA         NA
..  ..          ..              ..         ..
100 NA          NA              NA         NA

Thanks

Answers


A data.frame is just a list with some added attributes. Just coerce it:

> specimens <- list(A=runif(10),B=runif(10))
> 
> as.data.frame(specimens)
           A         B
1  0.6746436 0.7599987
2  0.2198677 0.5004017
3  0.4927745 0.9455003
4  0.8028011 0.8718274
5  0.6190707 0.7415874
6  0.5273992 0.8118802
7  0.6602548 0.4432799
8  0.5820781 0.8117375
9  0.8196531 0.5172833
10 0.0683938 0.0205693

Edit: Re-reading your problem, I suspect specimens is a character vector not really a list. If so:

N.rows <- 10
specimens <- c("A","B")
spec.dat <- as.data.frame(matrix(NA,nrow=N.rows,ncol=length(specimens)))
colnames(spec.dat) <- specimens

> spec.dat
    A  B
1  NA NA
2  NA NA
3  NA NA
4  NA NA
5  NA NA
6  NA NA
7  NA NA
8  NA NA
9  NA NA
10 NA NA

Need Your Help

Roles - Issue with User.IsInRole

asp.net role

I have a register page where the user is assigned to a role as follows once the user clicks on the submit button:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.