littlely
littlely

Reputation: 1418

what does lapply(Output_data,"[[",2) mean in R

In RHadoop, when we make the results readable, it will use the code:

results <- data.frame(words=unlist(lapply(Output_data,"[[",1)), count
=unlist(lapply(Output_data,"[[",2)))

but what does lapply(Output_data,"[[",1)mean? especially the "[[" and '1' in lapply.

Upvotes: 1

Views: 73

Answers (2)

Feng
Feng

Reputation: 613

Operators like [[ , [ and -> are actually functions.

list[[1]]

is equal to

`[[`(list,1)

In your case, lapply(Output_data,"[[",1)means to extract the first value of every column (or sublist) of Output_data. And the 1 is a argument passed to [[ function.

Upvotes: 1

akrun
akrun

Reputation: 886938

The syntax of extracting list elements with [ or [[ is often used in R. It is not specific to any packages. The meaning of the syntax

lapply(Output_data,"[[",1)

is loop through the object 'Output_data' and extract ([[) the first element. So, if the 'Output_data' is a list of data.frames, it will extract the first column of the data.frame and if it is a list of vectors, it extracts the first elements of vector. It does similar functionality as an anonymous function does i..e

lapply(Output_data, function(x) x[[1]]) 

The latter syntax is more clear and easier to understand but the former is compact and a bit more stylish...

More info about the [[ can be found in ?Extract

Upvotes: 1

Related Questions