thi*_*ick 25 r dollar-sign dataframe
我有这个示例代码从现有数据框'my_data'创建一个新的数据框'new_data'.
new_data = NULL
n = 10 #this number correspond to the number of rows in my_data
conditions = c("Bas_A", "Bas_T", "Oper_A", "Oper_T") # the vector characters correspond to the target column names in my_data
for (cond in conditions){
    for (i in 1:n){
        new_data <- rbind(new_data, c(cond, my_data$cond[i]))
    }
}
Run Code Online (Sandbox Code Playgroud)
问题是my_data$cond(其中cond是变量,而不是列名)不被接受.
如何在美元符号后面使用变量值来调用数据框的列?
Sve*_*ein 38
要访问列,请使用:
my_data[ , cond]
Run Code Online (Sandbox Code Playgroud)
要么
my_data[[cond]]
Run Code Online (Sandbox Code Playgroud)
第一i行可以通过以下方式访问:
my_data[i, ]
Run Code Online (Sandbox Code Playgroud)
结合两者以获得所需的值:
my_data[i, cond]
Run Code Online (Sandbox Code Playgroud)
要么
my_data[[cond]][i]
Run Code Online (Sandbox Code Playgroud)
        我猜你需要get()。
例如,
get(x,list),其中list是列表,x是变量(可以是字符串),等于list$x。
但在 中get(x,list),x可以是变量,而使用$,x则不能是变量。