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,060 points
60 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,010 points

Related Questions In Data Analytics

0 votes
1 answer

Define a SQL query? What is the difference between SELECT and UPDATE Query? How do you use SQL in SAS?

Structured query language (SQL) is a programming ...READ MORE

answered Aug 24, 2018 in Data Analytics by ANMOL
• 3,620 points
55 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,690 points
54 views
0 votes
1 answer
0 votes
1 answer
0 votes
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,700 points
47 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,700 points
41 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,060 points

edited Apr 12, 2018 by nirvana 1,841 views
0 votes
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 darklord
• 6,140 points
90 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,010 points
32 views
0 votes
1 answer