My list (sector_region
) contains 2 elements. In those 2 elements there are multiple elements. How can I fetch these elements?
Following is my piece of code.
sector_region = unique(universe_database[, c("Region", "Sector")])
My list (sector_region
) contains 2 elements. In those 2 elements there are multiple elements. How can I fetch these elements?
Following is my piece of code.
sector_region = unique(universe_database[, c("Region", "Sector")])
I quote your comment below:
Let's say : employee <- c('John Doe','Peter Gynn','Jolie Hope','John Doe','Peter Gynn','Jolie Hope','John Doe','Peter Gynn','Jolie Hope','John Doe','Peter Gynn','Jolie Hope') salary <- c(21000, 23400, 26800,21000, 23400, 26800,21000, 23400, 26800,21000, 23400, 26800) universe_database <- data.frame(employee, salary,stringsAsFactors=FALSE) sector_region = unique(universe_database[,c("employee","salary")]) Now sector_region is a list. If I do sector_region[1] I will get a list of elements. How can I get single element like 'John Doe'?
First of all, sector_region
is a data frame, not a list. You can confirm that with class(sector_region)
. By the way, your code is equivalent to sector_region = unique(universe_database)
since you only have two columns for universe_database
.
To retrieve "John Doe" here, simply execute sector_region$employee[1]
or sector_region[1, 1]
.
A more general solution would be sector_region$employee[sector_region$employee %in% "John Doe"]
. Or, if you want to keep the returned values in a data frame, do subset(sector_region, employee %in% "John Doe", select = employee)
.