【问题标题】:Accessing a dataframe using an index vs. using a variable in R使用索引访问数据框与使用 R 中的变量
【发布时间】:2015-05-09 07:11:35
【问题描述】:

我有两种方法可以访问 Excel 文件中的数据。第一种方法很简单:

easy.way <- read.csv(file="flanker1.csv",header=TRUE)

另一个是带索引的:

file.number <- c(1)
index.way <-setNames(lapply(paste0("flanker", file.number,".csv"),read.csv),paste0(file.number,'participant'))

两个输出看起来相同(我无法发布图片,因为我的声誉太低)。当我尝试访问数据时会出现此问题。例如:

length(test$block) # length = 400, works
length(data[1]$block) # length = 0, doesn't work

为什么我使用索引无法访问数据框的信息,但使用标准变量没有问题?

编辑:

str(测试):

'data.frame':   400 obs. of  10 variables:
 $ trial               : int  0 1 2 3 4 5 6 7 8 9 ...
 $ distractor.direction: int  2 1 1 2 2 1 1 2 2 2 ...
 $ target.direction    : int  1 2 2 1 2 1 2 1 1 1 ...
 $ fixcross.time       : int  2 1 2 3 2 4 2 2 2 2 ...
 $ intertrial.interval : int  2 1 1 2 1 4 3 3 4 1 ...
 $ type                : Factor w/ 2 levels "congruent","incongruent": 2 2 2 2 2 1 2 2 2 2 ...
 $ keypress            : Factor w/ 2 levels "['a']","['l']": 1 1 1 1 1 2 1 2 2 2 ...
 $ accuracy            : Factor w/ 2 levels "correct","incorrect": 2 1 1 2 1 1 1 1 1 1 ...
 $ rt                  : num  0.325 0.433 0.359 0.315 0.501 ...
 $ block               : Factor w/ 2 levels "delay","no delay": 1 1 1 1 1 1 1 1 1 1 ...

str(数据[1]):

List of 1
$ 1 participant:'data.frame':   400 obs. of  10 variables:
..$ trial               : int [1:400] 0 1 2 3 4 5 6 7 8 9 ...
..$ distractor.direction: int [1:400] 2 1 1 2 2 1 1 2 2 2 ...
..$ target.direction    : int [1:400] 1 2 2 1 2 1 2 1 1 1 ...
..$ fixcross.time       : int [1:400] 2 1 2 3 2 4 2 2 2 2 ...
..$ intertrial.interval : int [1:400] 2 1 1 2 1 4 3 3 4 1 ...
..$ type                : chr [1:400] "incongruent" "incongruent" "incongruent" "incongruent" ...
..$ keypress            : chr [1:400] "['a']" "['a']" "['a']" "['a']" ...
..$ accuracy            : chr [1:400] "incorrect" "correct" "correct" "incorrect" ...
..$ rt                  : num [1:400] 0.325 0.433 0.359 0.315 0.501 ...
..$ block               : chr [1:400] "delay" "delay" "delay" "delay" ...

【问题讨论】:

  • 你能提供一个一致的例子吗?目前尚不清楚您尝试使用 length() 访问什么。另外,两种方式的 Str() 返回什么?
  • 可能data[1] 没有“块”作为名称。 str(data[1]) 向您展示了什么?这个问题非常不连贯,因为测试和数据都没有以任何方式定义或描述。
  • 我认为您需要访问 data 中的列表元素。 data[[1]]$block 工作。注意双括号[[
  • 是的,我刚刚意识到 - 谢谢。如果您想将其添加到答案部分,我会给您打勾。

标签: r excel indexing dataframe


【解决方案1】:

我认为您需要使用双括号表示法。 data[[1]]$block 应该可以工作

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    • 2016-09-28
    • 1970-01-01
    • 2020-05-10
    相关资源
    最近更新 更多