All Questions

Tagged with
Filter by
Sorted by
Tagged with
675 votes
11 answers
333k views

The difference between bracket [ ] and double bracket [[ ]] for accessing the elements of a list or dataframe

R provides two different methods for accessing the elements of a list or data.frame: [] and [[]]. What is the difference between the two, and when should I use one over the other?
Sharpie's user avatar
  • 17.5k
642 votes
27 answers
1.2m views

Convert a list to a data frame

I have a nested list of data. Its length is 132 and each item is a list of length 20. Is there a quick way to convert this structure into a data frame that has 132 rows and 20 columns of data? Here is ...
Btibert3's user avatar
  • 39.4k
500 votes
11 answers
358k views

Combine a list of data frames into one data frame by row

I have code that at one place ends up with a list of data frames which I really want to convert to a single big data frame. I got some pointers from an earlier question which was trying to do ...
JD Long's user avatar
  • 60.3k
366 votes
20 answers
683k views

How can I remove an element from a list?

I have a list and I want to remove a single element from it. How can I do this? I've tried looking up what I think the obvious names for this function would be in the reference manual and I haven't ...
David Locke's user avatar
346 votes
13 answers
219k views

How to correctly use lists?

Brief background: Many (most?) contemporary programming languages in widespread use have at least a handful of ADTs [abstract data types] in common, in particular, string (a sequence comprised of ...
doug's user avatar
  • 69.6k
343 votes
9 answers
283k views

Simultaneously merge multiple data.frames in a list

I have a list of many data.frames that I want to merge. The issue here is that each data.frame differs in terms of the number of rows and columns, but they all share the key variables (which I've ...
bshor's user avatar
  • 4,919
270 votes
10 answers
378k views

How do I make a list of data frames?

How do I make a list of data frames and how do I access each of those data frames from the list? For example, how can I put these data frames in a list ? d1 <- data.frame(y1 = c(1, 2, 3), ...
Ben's user avatar
  • 20.8k
257 votes
17 answers
310k views

Append an object to a list in R in amortized constant time, O(1)?

If I have some R list mylist, you can append an item obj to it like so: mylist[[length(mylist)+1]] <- obj But surely there is some more compact way. When I was new at R, I tried writing lappend(...
Nick's user avatar
  • 21.8k
195 votes
9 answers
660k views

Append value to empty vector in R?

I'm trying to learn R and I can't figure out how to append to a list. If this were Python I would . . . #Python vector = [] values = ['a','b','c','d','e','f','g'] for i in range(0,len(values)): ...
O.rka's user avatar
  • 30.6k
175 votes
14 answers
255k views

data.frame rows to a list

I have a data.frame which I would like to convert to a list by rows, meaning each row would correspond to its own list elements. In other words, I would like a list that is as long as the data.frame ...
Roman Luštrik's user avatar
169 votes
5 answers
256k views

How to combine two lists in R

I have two lists: l1 = list(2, 3) l2 = list(4) I want a third list: list(2, 3, 4). How can I do it in simple way. Although I can do it in for loop, but I am expecting a one liner answer, or maybe ...
Rohit Singh's user avatar
  • 2,151
151 votes
5 answers
119k views

What are the differences between vector and list data types in R?

What are the main differences between vector and list data types in R? What are the advantages or disadvantages of using (or not) these two data types? I would appreciate seeing examples that ...
DCR's user avatar
  • 1,519
124 votes
8 answers
159k views

Creating an R dataframe row-by-row

I would like to construct a dataframe row-by-row in R. I've done some searching, and all I came up with is the suggestion to create an empty list, keep a list index scalar, then each time add to the ...
David B's user avatar
  • 29.6k
117 votes
5 answers
324k views

R: Count number of objects in list [closed]

Can someone recommend a function that can allow me to count and return the number of items in a list? library(stringr) l <- strsplit(words, "a") if(# number of items in list l < 1) ...
Karl's user avatar
  • 5,708
115 votes
5 answers
119k views

Select first element of nested list

Let's say I have a list like this: x = list(list(1,2), list(3,4), list(5,6)) I would like a list that contains only the first elements of the nested list. I can do this by returning another list ...
Alex's user avatar
  • 19.8k
115 votes
2 answers
177k views

Select multiple elements from a list

I have a list in R some 10,000 elements long. Say I want to select only elements, 5, 7, and 9. I'm not sure how I would do that without a for loop. I want to do something like mylist[[c(5,7,9]] but ...
user1357015's user avatar
  • 11.5k
109 votes
3 answers
186k views

Read all files in a folder and apply a function to each data frame

I am doing a relatively simple piece of analysis that I have put into a function on all the files in a particular folder. I was wondering whether anyone had any tips to help me automate the process on ...
KT_1's user avatar
  • 8,374
102 votes
7 answers
160k views

Working with dictionaries/lists to get list of keys

I have trivial question: I couldn't find a dictionary data structure in R, so I used list instead (like "word"->number). So, how do I get the list of keys.
Ivri's user avatar
  • 2,199
102 votes
5 answers
56k views

Create a data.frame where a column is a list

I know how to add a list column: > df <- data.frame(a=1:3) > df$b <- list(1:1, 1:2, 1:3) > df a b 1 1 1 2 2 1, 2 3 3 1, 2, 3 This works, but not: > df <- data....
flodel's user avatar
  • 88.3k
94 votes
16 answers
106k views

Convert a matrix to a list of column-vectors

Say you want to convert a matrix to a list, where each element of the list contains one column. list() or as.list() obviously won't work, and until now I use a hack using the behaviour of tapply : x &...
Joris Meys's user avatar
  • 108k
94 votes
4 answers
96k views

How to flatten a list of lists?

The tm package extends c so that, if given a set of PlainTextDocuments it automatically creates a Corpus. Unfortunately, it appears that each PlainTextDocument must be specified separately. e.g. if ...
dnagirl's user avatar
  • 20.3k
83 votes
2 answers
60k views

What is difference between dataframe and list in R?

What is difference between dataframe and list in R? Which one should be used when? Which is easier to loop over? Exact problem: I have to first store 3 string elements like "a", "b", "c". Later for ...
ShazSimple's user avatar
82 votes
9 answers
121k views

Merge Two Lists in R

I have two lists first = list(a = 1, b = 2, c = 3) second = list(a = 2, b = 3, c = 4) I want to merge these two lists so the final product is $a [1] 1 2 $b [1] 2 3 $c [1] 3 4 Is there a simple ...
Michael's user avatar
  • 7,227
80 votes
7 answers
169k views

Appending a list to a list of lists in R

I'm having issues appending data to a list which is already in a list format. I have a program which will export results objects during a simulation loop. The data itself is stored as a list of ...
SJWard's user avatar
  • 3,689
80 votes
5 answers
32k views

Prevent unlist to drop NULL values

I have a vector of lists and I use unlist on them. Some of the elements in the vectors are NULL and unlist seems to be dropping them. How can I prevent this? Here's a simple (non) working example ...
nico's user avatar
  • 51.3k
77 votes
3 answers
139k views

Convert named list to vector with values only

I have a list of named values: myList <- list('A' = 1, 'B' = 2, 'C' = 3) I want a vector with the value 1:3 I can't figure out how to extract the values without defining a function. Is there a ...
sharoz's user avatar
  • 6,265
70 votes
5 answers
65k views

Remove empty elements from list with character(0)

How can I remove empty elements from a list that contain zero length pairlist as character(0), integer(0) etc... list2 # $`hsa:7476` # [1] "1","2","3" # # $`hsa:656` # character(0) # # $`hsa:7475` # ...
Richard A. Schäfer's user avatar
69 votes
3 answers
41k views

Sum a list of matrices [duplicate]

I have a list where each element is a 5*5 matrix. Eg [[1]] V1 V2 V3 V4 V5 [1,] 0.000000 46.973700 21.453500 338.547000 10.401600 ...
Seen's user avatar
  • 4,144
68 votes
5 answers
156k views

How to convert a huge list-of-vector to a matrix more efficiently?

I have a list of length 130,000 where each element is a character vector of length 110. I would like to convert this list to a matrix with dimension 1,430,000*10. How can I do it more efficiently?\ My ...
user1787675's user avatar
  • 1,111
65 votes
6 answers
71k views

R + combine a list of vectors into a single vector

I have a single list of numeric vector and I want to combine them into one vector. But I am unable to do that. This list can have one element common across the list element. Final vector should not ...
Rachit Agrawal's user avatar
64 votes
3 answers
43k views

Creating a named list from two vectors (names, values)

Is there a way to use mapply on two vectors to construct a named list? The first vector would be of type character and contain the names used for the list while the second contains the values. So far,...
Jon Claus's user avatar
  • 2,892
64 votes
4 answers
87k views

How to add variable key/value pair to list object?

I have two variables, key and value, and I want to add them as a key/value pair to a list: key = "width" value = 32 mylist = list() mylist$key = value The result is this: mylist # $key # [1] 32 ...
Frank's user avatar
  • 65.2k
62 votes
5 answers
50k views

Assigning NULL to a list element in R?

I found this behaviour odd and wanted more experienced users to share their thoughts and workarounds. On running the code sample below in R: sampleList <- list() d<- data.frame(x1 = letters[1:...
harshsinghal's user avatar
  • 3,760
58 votes
4 answers
28k views

preallocate list in R

It is inefficient in R to expand a data structure in a loop. How do I preallocate a list of a certain size? matrix makes this easy via the ncol and nrow arguments. How does one do this in lists? ...
Alex's user avatar
  • 19.8k
57 votes
3 answers
19k views

Unpacking argument lists for ellipsis in R

I am confused by the use of the ellipsis (...) in some functions, i.e. how to pass an object containing the arguments as a single argument. In Python it is called "unpacking argument lists", e.g. &...
mhermans's user avatar
  • 2,147
54 votes
8 answers
125k views

R: Print list to a text file

I have in R a list like this: > print(head(mylist,2)) [[1]] [1] 234984 10354 41175 932711 426928 [[2]] [1] 1693237 13462 Each element of the list has different number of its elements. I ...
pms's user avatar
  • 4,566
54 votes
7 answers
8k views

How to flatten a list to a list without coercion?

I am trying to achieve the functionality similar to unlist, with the exception that types are not coerced to a vector, but the list with preserved types is returned instead. For instance: flatten(...
eold's user avatar
  • 6,012
52 votes
2 answers
25k views

How to remove a level of lists from a list of lists

I have created some lists within a list and would like to be able have each sublist element to be an individual element at the top level. For example to create some dummy data: pp <- lapply(10:15,...
h.l.m's user avatar
  • 13.3k
52 votes
3 answers
81k views

Here we go again: append an element to a list in R

I am not happy with the accepted answer to Append an object to a list in R in amortized constant time? > list1 <- list("foo", pi) > bar <- list("A", "B") How can I append new element bar ...
user443854's user avatar
  • 7,286
50 votes
4 answers
56k views

Convert list of vectors to data frame

I'm trying to convert a list of vectors (a multidimensional array essentially) into a data frame, but every time I try I'm getting unexpected results. My aim is to instantiate a blank list, populate ...
Nick's user avatar
  • 839
50 votes
4 answers
15k views

Fast vectorized merge of list of data.frames by row

Most of the questions about merging data.frame in lists on SO don't quite relate to what I'm trying to get across here, but feel free to prove me wrong. I have a list of data.frames. I would like to "...
Roman Luštrik's user avatar
48 votes
7 answers
61k views

What is the most efficient way to cast a list as a data frame?

Very often I want to convert a list wherein each index has identical element types to a data frame. For example, I may have a list: > my.list [[1]] [[1]]$global_stdev_ppb [1] 24267673 [[1]]$...
DrewConway's user avatar
  • 5,447
48 votes
2 answers
15k views

What is the difference between a list and a pairlist in R?

In reading the documentation for lists, I found references to pairlists, but it wasn't clear to me how they were different from lists.
Christopher Bottoms's user avatar
46 votes
4 answers
5k views

Can lists be created that name themselves based on input object names?

It would be very helpful to me to be able to create an R list object without having to specify the names of each element. For example: a1 <- 1 a2 <- 20 a3 <- 1:20 b <- list(a1,a2,a3, ...
Francis Smart's user avatar
44 votes
8 answers
62k views

Subset elements in a list based on a logical condition

How can I subset a list based on a condition (TRUE, FALSE) in another list? Please, see my example below: l <- list(a=c(1,2,3), b=c(4,5,6,5), c=c(3,4,5,6)) l $a [1] 1 2 3 $b [1] 4 5 6 5 $c [1] 3 ...
jrara's user avatar
  • 16.6k
43 votes
3 answers
20k views

Use input of purrr's map function to create a named list as output in R

I am using the map function of the purrr package in R which gives as output a list. Now I would like the output to be a named list based on the input. An example is given below. input <- c("a", "b"...
Michael's user avatar
  • 1,301
43 votes
2 answers
139k views

Naming list elements in R

I've been doing some work with some large, complex lists lately and I've seen some behaviour which was surprising (to me, at least), mainly to do with assigning names to a list. A simple example: Fil ...
RobertMyles's user avatar
  • 2,743
43 votes
7 answers
32k views

Is there a more efficient way to replace NULL with NA in a list?

I quite often come across data that is structured something like this: employees <- list( list(id = 1, dept = "IT", age = 29, sportsteam = "softball"), ...
Jon M's user avatar
  • 1,167
43 votes
1 answer
1k views

R: Why is the [[ ]] approach for subsetting a list faster than using $?

I've been working on a few projects that have required me to do a lot of list subsetting and while profiling code I realised that the object[["nameHere"]] approach to subsetting lists was usually ...
Jon M's user avatar
  • 1,167
42 votes
5 answers
45k views

Recombining a list of Data.frames into a single data frame [duplicate]

I am sorry if this question has been answered already. Also, this is my first time on stackoverflow. I have a beginner R question concerning lists , data frames and merge() and/or rbind(). I ...
CGN's user avatar
  • 697

1
2 3 4 5
166