What is the difference between [] and [[]] notations to access the elements of a list or dataframe in R?

0 votes

As R provides two different ways to access the elements of a list or a data.frame, what is the difference between both of them?

In which situations should I use[] over [[]]?

Apr 12, 2018 in Data Analytics by nirvana
• 3,120 points
773 views

1 answer to this question.

0 votes

R provides 3 basic indexing operators. Refer below for the syntax and example:

    a[i]
    a[i, j]
    a[[i]]
    a[[i, j]]
    a$x
    a$"x"

When it comes to vectors and matrices [[ are hardly used even though they have semantic differences from [.

While indexing multi-dimensional structures with a single index, a[[i]] or a[i] will return the ith sequential element of a.

When you use lists, [[ is used to select any single element, and [ is used to return a list of the selected elements.

[[ allows a single element to be selected using various indices like integer/character and [ indexes by vectors.

answered Apr 12, 2018 by kappa3010
• 2,090 points

Related Questions In Data Analytics

0 votes
1 answer

What is the difference between list and vector in R?

The difference are - A list holds different ...READ MORE

answered Oct 28, 2019 in Data Analytics by Cherukuri
• 32,490 points
10,864 views
0 votes
1 answer

What is the difference between library () and require () functions in R ?

 library() require() Library () function gives an error message ...READ MORE

answered Sep 5, 2018 in Data Analytics by zombie
• 3,770 points
558 views
0 votes
2 answers

What is the difference between %% and % in R programming?

HI, %% returns remainder in case of numeric ...READ MORE

answered Aug 26, 2019 in Data Analytics by anonymous
• 32,490 points
411 views
0 votes
1 answer

What is the difference between merge and bind in R?

Merge function merges an arbitrarily large series ...READ MORE

answered Oct 28, 2019 in Data Analytics by Cherukuri
• 32,490 points
1,376 views
+1 vote
1 answer

How to convert a list of vectors with various length into a Data.Frame?

We can easily use this command as.data.frame(lapply(d1, "length< ...READ MORE

answered Apr 4, 2018 in Data Analytics by DeepCoder786
• 1,720 points
420 views
0 votes
1 answer

How to create a list of Data frames?

Basically all we have to do is ...READ MORE

answered Apr 9, 2018 in Data Analytics by DeepCoder786
• 1,720 points
317 views
0 votes
1 answer

How to convert a list to data frame in R?

Let's assume your list of lists is ...READ MORE

answered Apr 12, 2018 in Data Analytics by nirvana
• 3,120 points

edited Apr 12, 2018 by nirvana 15,882 views
+1 vote
1 answer

How to convert a list of dataframes in to a single dataframe using R?

You can use the plyr function: data <- ...READ MORE

answered Apr 13, 2018 in Data Analytics by Sahiti
• 6,320 points
1,228 views
0 votes
1 answer

How to limit output of a dataframe in R?

For randomly sampling a row/cell where a ...READ MORE

answered Apr 18, 2018 in Data Analytics by kappa3010
• 2,090 points
234 views
0 votes
1 answer