0

I have a list of 6 lists:

> dput(ATQ1)
list(c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 
0, 0), c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1), c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1), c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1), c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1), c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 
1, 1))

I want to write a short program to name them (for instance, col1, col2, col3, col4, col5, col6.) and then create a data frame using this data where each list represent a column of a data frame. Can someone let me know how to do it? I searched for a solution but couldn't get it. Thanks.

Sumit
  • 2,242
  • 4
  • 25
  • 43
  • `as.data.frame` and `names` are you friends here, the first converts the list to a data.frame and the second allows you to name the columns. – Sam Mason Nov 20 '13 at 12:53
  • Oh wow! Cool! I never knew that it is not required for a data frame to have column names. So it seems, a data frame is just a list of lists of homogeneous size. – Sumit Nov 20 '13 at 12:56
  • @JoshuaUlrich looks to be, just did a search using obvious terms and found links to that post as well… looks sensible, if a bit complicated for a new user of R as the OP seems to be – Sam Mason Nov 20 '13 at 13:31
  • @SamMason: yes, that's complicated for efficiency purposes; but that question also has good links to other, less complicated, questions. – Joshua Ulrich Nov 20 '13 at 13:40
  • I looked at the answer. I am going to use attributes, as those seem to work faster and are efficient. – Sumit Nov 20 '13 at 13:44
  • @Sumit, the response of @user1981275 is fine unless you're dealing with *very* large `data.frame`s. – Sam Mason Nov 20 '13 at 15:18
  • Yes, I am using the one he mentioned. – Sumit Nov 20 '13 at 17:10

1 Answers1

1

You can name your list l as follows:

names(l) -> c("A", "B", "C", "D", "E", "F")

and then create a data.frame:

> data.frame(l)
   A B C D E F
1  1 1 1 1 1 1
2  1 1 1 1 1 1
3  1 1 1 1 1 1
4  1 1 1 1 1 1
5  1 1 1 1 1 1
6  1 1 1 1 1 1
7  1 1 1 1 1 1
8  1 1 1 1 1 1
9  1 1 1 1 1 1
10 1 1 1 1 1 1
11 1 1 1 1 1 1
12 1 1 1 1 1 1
13 1 1 1 1 1 1
14 0 1 1 1 1 1
15 0 1 1 1 1 1
16 0 1 1 1 1 1
17 0 1 1 1 1 1
18 0 1 1 1 1 1
19 0 1 1 1 1 1
20 0 1 1 1 1 1

as pointed out already in the comment of Sam Mason

user1981275
  • 13,002
  • 8
  • 72
  • 101