[英]In R, How to select a column in a dataframe by using a variable?
I am trying to get the column of a dataframe using a variable.我正在尝试使用变量获取 dataframe 的列。 However, there does not seems to be anyway in R to do so.
但是,R 中似乎没有这样做。 How to get column of a dataframe by using a variable in R?
如何通过使用 R 中的变量来获取 dataframe 的列?
An idea of what I am trying to do:我想做什么的想法:
getcolumn <- function(file_link, column)
{
#get column
employee_df=read.csv(file_link, header=TRUE)
column_data=data.frame(employee_df[,column]) #How to get column name based on the value in the variable?
print(column_data)
}
getcolumn("/home/Documents/Employee.xsl","Name")
Try using drop = F
:尝试使用
drop = F
:
getcolumn <- function(file_link, column)
{
#get column
employee_df=read.csv(file_link, header=TRUE)
column_data=employee_df[,column , drop = F] #How to get column name based on the value in the variable?
print(column_data)
}
getcolumn("/home/Documents/Employee.xsl","Name")
This should work as long as Name
is a column in your csv file.只要
Name
是 csv 文件中的一列,这应该可以工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.