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
143 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,020 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 in Data Analytics by Cherukuri
• 32,260 points
290 views
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
241 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,710 points
102 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 in Data Analytics by anonymous
• 32,260 points
93 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
135 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
113 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 3,996 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 darklord
• 6,190 points
217 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,020 points
65 views
0 votes
1 answer