简体   繁体   中英

How to use a list of row numbers to look up values in a dataframe column

I have a big dataframe containing to columns, one that is an ID Code named "code" and one that is the name of two train stations separated by a slash named "name"

I wanted to search all the codes associated to a station name (and being able to lookup multiple stations at a time) so it would give me a list of vectors containing the multiple codes for each station.

I used lapply to get the rows for each station but now I'm unable to look up the value in the column "code" associated with the row number.

SearchFor <- c("Chicago", "New York", "Atlanta")
lapply(c(SearchFor,grep,x=datastations$name)

I have the following list:

$`Chicago`
 [1]  29  64 135 160 164 167 176 186 225 247 248 

$New York
 [1]  51  53 109 111 112 164 


$Atlanta
[1]   4  78 168 237 291

Basically, I'd want to change each of these numbers into the value of the column "code" at these rows.

Here's my datatable "datastations" after I used dput:

structure(list(code = c(6000L, 6001L, 6002L, 6003L, 6004L, 6005L, 
6006L, 6007L, 6008L, 6009L, 6010L, 6011L, 6012L, 6013L, 6014L, 
6015L, 6016L, 6017L, 6018L, 6019L, 6020L, 6021L, 6022L, 6023L, 
6024L, 6025L, 6026L, 6027L, 6028L, 6029L, 6030L, 6031L, 6032L, 
6033L, 6034L, 6035L, 6036L, 6037L, 6038L, 6039L, 6040L, 6041L, 
6042L, 6043L, 6044L, 6045L, 6046L, 6047L, 6048L, 6049L, 5000L, 
5001L, 5002L, 5003L, 5004L, 5005L, 5006L, 5007L, 5008L, 6050L, 
6051L, 6052L, 6053L, 6054L, 6055L, 6056L, 6057L, 6058L, 6059L, 
6060L, 6061L, 6062L, 6063L, 6064L, 6065L, 6066L, 6067L, 6068L, 
6069L, 6070L, 6071L, 6072L, 6073L, 6074L, 6075L, 6076L, 6077L, 
6078L, 6079L, 6080L, 6081L, 6082L, 6083L, 6084L, 6085L, 6086L, 
6087L, 6088L, 6089L, 6090L, 6091L, 5009L, 5010L, 5011L, 5012L, 
6092L, 6093L, 6094L, 6095L, 6096L, 6097L), name = c("Atlanta / New York", 
"Atlanta / Chicago", "Atlanta / Miami", "Atlanta / Los Angeles", 
"Atlanta / Toronto", "Atlanta / Washington", "Atlanta / Cleveland", 
"Atlanta / Raleigh", "Atlanta / Newark", "Atlanta / Ottawa", 
"Atlanta / Detroit", "Atlanta / Albany", "Atlanta / Hartford", 
"Atlanta / Providence", "New York / Chicago", "New York / Miami", 
"New York / Los Angeles", "New York / Toronto", "New York / Washington", 
"New York / Cleveland", "New York / Raleigh", "New York / Newark", 
"New York / Ottawa", "New York / Detroit", "New York / Albany", 
"New York / Hartford", "New York / Providence", "Chicago / Miami", 
"Chicago / Los Angeles", "Chicago / Toronto", "Chicago / Washington", 
"Chicago / Cleveland", "Chicago / Raleigh", "Chicago / Newark", 
"Chicago / Ottawa", "Chicago / Detroit", "Chicago / Albany", 
"Chicago / Hartford", "Chicago / Providence", "Miami / Los Angeles", 
"Miami / Toronto", "Miami / Washington", "Miami / Cleveland", 
"Miami / Raleigh", "Miami / Newark", "Miami / Ottawa", "Miami / Detroit", 
"Miami / Albany", "Miami / Hartford", "Miami / Providence", "Toronto /             Washington", 
"Toronto / Cleveland", "Toronto / Raleigh", "Toronto / Newark", 
"Toronto / Ottawa", "Toronto / Detroit", "Toronto / Albany", 
"Toronto / Hartford", "Toronto / Providence", "Los Angeles / Toronto", 
"Los Angeles / Washington", "Los Angeles / Cleveland", "Los Angeles /         Raleigh", 
"Los Angeles / Newark", "Los Angeles / Ottawa", "Los Angeles / Detroit", 
"Los Angeles / Albany", "Los Angeles / Hartford", "Los Angeles / Providence", 
"Washington / Washington", "Washington / Cleveland", "Washington / Raleigh", 
"Washington / Newark", "Washington / Ottawa", "Washington / Detroit", 
"Washington / Hartford", "Washington / Providence", "Raleigh / Newark", 
"Raleigh / Ottawa", "Raleigh / Detroit", "Raleigh / Albany", 
"Raleigh / Hartford", "Raleigh / Providence", "Cleveland / Raleigh", 
"Cleveland / Newark", "Cleveland / Ottawa", "Cleveland / Detroit", 
"Cleveland / Albany", "Cleveland / Hartford", "Cleveland / Providence", 
"New York / Newark", "New York / Ottawa", "New York / Detroit", 
"New York / Albany", "New York / Hartford", "New York / Providence", 
"Newark / Ottawa", "Newark / Detroit", "Newark / Albany", "Newark /         Hartford", 
"Newark / Providence", "Ottawa / Detroit", "Ottawa / Albany", 
"Ottawa / Hartford", "Ottawa / Providence", "Detroit / Albany", 
"Detroit / Hartford", "Detroit / Providence", "Albany / Hartford", 
"Albany / Providence", "Hartford / Providence")), class = "data.frame",     row.names = c(NA, 
-111L))

I got this database by reading a .csv file using this code

read.csv(file, colClasses = 
c(rep("integer",1),rep("character",1),rep("NULL",2)))

I'd like to apply something like :

List[1] <- datastations$code[List[[1]]]

but on every vector of the list, no matter how many there are (so no loop basically)

Maybe this is what you're looking for? The way I read the question, you need a list of all the station codes that correspond to a particular city or group of cities. It looks like maybe the wrong station codes got in your dput if this looks funny.

library(dplyr)
codelist <- df %>% filter(grepl("Chicago",name)) %>% select(code)

> unlist(codelist)
 code1  code2  code3  code4  code5  code6  code7  code8  code9 code10 code11 code12 code13 code14 
  6001   6014   6027   6028   6029   6030   6031   6032   6033   6034   6035   6036   6037   6038 

Or for multiple stations:

> codelist <- df %>% filter(grepl("Chicago|New York|Atlanta",name)) %>% select(code)
> unlist(codelist)
 code1  code2  code3  code4  code5  code6  code7  code8  code9 code10 code11 code12 code13 code14 code15 
  6000   6001   6002   6003   6004   6005   6006   6007   6008   6009   6010   6011   6012   6013   6014 
code16 code17 code18 code19 code20 code21 code22 code23 code24 code25 code26 code27 code28 code29 code30 
  6015   6016   6017   6018   6019   6020   6021   6022   6023   6024   6025   6026   6027   6028   6029 
code31 code32 code33 code34 code35 code36 code37 code38 code39 code40 code41 code42 code43 code44 code45 
  6030   6031   6032   6033   6034   6035   6036   6037   6038   6081   6082   6083   6084   6085   6086 

Like others have said in comments above, it's not entirely clear what the end result is that you want. But if I understand correctly, I think this might be what you want.

Here I'm using map from the package purrr to iterate over a vector of city names and get a vector of codes for each one, the use set_names to name the elements of the final list by the cities.

library(dplyr)
library(stringr)
library(purrr)

# load data as df (see below) 

cities <- c("Chicago", "New York", "Atlanta")

get_city_stations <- function(city, station_data) {
  station_data %>% 
    filter(str_detect(name, city)) %>% 
    pull(code)
}

codes <- map(cities, get_city_stations, station_data = df) %>% set_names(cities)

codes
#> $Chicago
#>  [1] 6001 6014 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038
#> 
#> $`New York`
#>  [1] 6000 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026
#> [15] 6081 6082 6083 6084 6085 6086
#> 
#> $Atlanta
#>  [1] 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013

Created on 2018-10-14 by the reprex package (v0.2.0).

df <- structure(list(code = c(6000L, 6001L, 6002L, 6003L, 6004L, 6005L, 
6006L, 6007L, 6008L, 6009L, 6010L, 6011L, 6012L, 6013L, 6014L, 
6015L, 6016L, 6017L, 6018L, 6019L, 6020L, 6021L, 6022L, 6023L, 
6024L, 6025L, 6026L, 6027L, 6028L, 6029L, 6030L, 6031L, 6032L, 
6033L, 6034L, 6035L, 6036L, 6037L, 6038L, 6039L, 6040L, 6041L, 
6042L, 6043L, 6044L, 6045L, 6046L, 6047L, 6048L, 6049L, 5000L, 
5001L, 5002L, 5003L, 5004L, 5005L, 5006L, 5007L, 5008L, 6050L, 
6051L, 6052L, 6053L, 6054L, 6055L, 6056L, 6057L, 6058L, 6059L, 
6060L, 6061L, 6062L, 6063L, 6064L, 6065L, 6066L, 6067L, 6068L, 
6069L, 6070L, 6071L, 6072L, 6073L, 6074L, 6075L, 6076L, 6077L, 
6078L, 6079L, 6080L, 6081L, 6082L, 6083L, 6084L, 6085L, 6086L, 
6087L, 6088L, 6089L, 6090L, 6091L, 5009L, 5010L, 5011L, 5012L, 
6092L, 6093L, 6094L, 6095L, 6096L, 6097L), name = c("Atlanta / New York", 
"Atlanta / Chicago", "Atlanta / Miami", "Atlanta / Los Angeles", 
"Atlanta / Toronto", "Atlanta / Washington", "Atlanta / Cleveland", 
"Atlanta / Raleigh", "Atlanta / Newark", "Atlanta / Ottawa", 
"Atlanta / Detroit", "Atlanta / Albany", "Atlanta / Hartford", 
"Atlanta / Providence", "New York / Chicago", "New York / Miami", 
"New York / Los Angeles", "New York / Toronto", "New York / Washington", 
"New York / Cleveland", "New York / Raleigh", "New York / Newark", 
"New York / Ottawa", "New York / Detroit", "New York / Albany", 
"New York / Hartford", "New York / Providence", "Chicago / Miami", 
"Chicago / Los Angeles", "Chicago / Toronto", "Chicago / Washington", 
"Chicago / Cleveland", "Chicago / Raleigh", "Chicago / Newark", 
"Chicago / Ottawa", "Chicago / Detroit", "Chicago / Albany", 
"Chicago / Hartford", "Chicago / Providence", "Miami / Los Angeles", 
"Miami / Toronto", "Miami / Washington", "Miami / Cleveland", 
"Miami / Raleigh", "Miami / Newark", "Miami / Ottawa", "Miami / Detroit", 
"Miami / Albany", "Miami / Hartford", "Miami / Providence", "Toronto /             Washington", 
"Toronto / Cleveland", "Toronto / Raleigh", "Toronto / Newark", 
"Toronto / Ottawa", "Toronto / Detroit", "Toronto / Albany", 
"Toronto / Hartford", "Toronto / Providence", "Los Angeles / Toronto", 
"Los Angeles / Washington", "Los Angeles / Cleveland", "Los Angeles /         Raleigh", 
"Los Angeles / Newark", "Los Angeles / Ottawa", "Los Angeles / Detroit", 
"Los Angeles / Albany", "Los Angeles / Hartford", "Los Angeles / Providence", 
"Washington / Washington", "Washington / Cleveland", "Washington / Raleigh", 
"Washington / Newark", "Washington / Ottawa", "Washington / Detroit", 
"Washington / Hartford", "Washington / Providence", "Raleigh / Newark", 
"Raleigh / Ottawa", "Raleigh / Detroit", "Raleigh / Albany", 
"Raleigh / Hartford", "Raleigh / Providence", "Cleveland / Raleigh", 
"Cleveland / Newark", "Cleveland / Ottawa", "Cleveland / Detroit", 
"Cleveland / Albany", "Cleveland / Hartford", "Cleveland / Providence", 
"New York / Newark", "New York / Ottawa", "New York / Detroit", 
"New York / Albany", "New York / Hartford", "New York / Providence", 
"Newark / Ottawa", "Newark / Detroit", "Newark / Albany", "Newark /         Hartford", 
"Newark / Providence", "Ottawa / Detroit", "Ottawa / Albany", 
"Ottawa / Hartford", "Ottawa / Providence", "Detroit / Albany", 
"Detroit / Hartford", "Detroit / Providence", "Albany / Hartford", 
"Albany / Providence", "Hartford / Providence")), class = "data.frame",     row.names = c(NA, 
-111L))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM