如何循环遍历数据框名称的向量以打印 df 的第一列?
How to loop through a vector of data frame names to print first columns of the df's?
所以 x 是一个向量。我正在尝试打印保存在向量中的 df 名称的第一列。到目前为止,我已经尝试了以下方法,但它们似乎不起作用。
x = (c('Ethereum,another Df..., another DF...,'))
for (i in x){
print(i[,1])
}
sapply(toString(Ethereum), function(i) print(i[1]))
您可以使用 mget
获取列表中的数据,并使用 lapply
提取列表中每个数据框的第一列。
data <- lapply(mget(x), `[`, 1)
#Use `[[` to get it as vector.
#data <- lapply(mget(x), `[[`, 1)
使用 purrr::map
的类似解决方案:
data <- purrr::map(mget(x), `[`, 1)
你可以试试这个
x <- c('Ethereum','anotherDf',...)
for (i in x){
print(get(i)[,1])
}
所以 x 是一个向量。我正在尝试打印保存在向量中的 df 名称的第一列。到目前为止,我已经尝试了以下方法,但它们似乎不起作用。
x = (c('Ethereum,another Df..., another DF...,'))
for (i in x){
print(i[,1])
}
sapply(toString(Ethereum), function(i) print(i[1]))
您可以使用 mget
获取列表中的数据,并使用 lapply
提取列表中每个数据框的第一列。
data <- lapply(mget(x), `[`, 1)
#Use `[[` to get it as vector.
#data <- lapply(mget(x), `[[`, 1)
使用 purrr::map
的类似解决方案:
data <- purrr::map(mget(x), `[`, 1)
你可以试试这个
x <- c('Ethereum','anotherDf',...)
for (i in x){
print(get(i)[,1])
}