0

so x is a vector. i am trying to print the first col of df's name's saved in the vector. so far I have tried the below but they don't seem to work.

x = (c('Ethereum,another Df..., another DF...,'))

for (i in x){
  print(i[,1])
}

sapply(toString(Ethereum), function(i) print(i[1]))
jogo
  • 12,469
  • 11
  • 37
  • 42
  • Not really clear what you need. Please give an example with expected output – Sotos Jun 17 '20 at 14:34
  • imagine x is vector where each item is a name of a data frame. now I want to print the first colm of each data frame. i would have to go df[,1] to print for each one. now imagine the vector has 100 data frame names. how would you print the first colm of each? I hope that makes sense. – Adetya Aggarwal Jun 17 '20 at 14:39
  • Your vector is a single string not a vector. Likely something like `strsplit(',', x)[[1]][1]` is what you're seeking. Or `get(strsplit(',', x)[[1]][1])[, 1]` – Oliver Jun 17 '20 at 14:40
  • its a vector -> x = c('Bitcoin','Tether','etc') – Adetya Aggarwal Jun 17 '20 at 14:44

2 Answers2

2

You can try this

x <- c('Ethereum','anotherDf',...)

for (i in x){
  print(get(i)[,1])
}
englealuze
  • 1,445
  • 12
  • 19
1

You can use mget to get data in a list and using lapply extract the first column of each dataframe in the list.

data <- lapply(mget(x), `[`, 1)
#Use `[[` to get it as vector. 
#data <- lapply(mget(x), `[[`, 1)

Similar solution using purrr::map :

data <- purrr::map(mget(x), `[`, 1)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213