使用索引访问数据帧与使用 R 中的变量
Accessing a dataframe using an index vs. using a variable in R
我有两种方法可以访问 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'))
两个输出看起来一样(我无法 post 图片,因为我的声誉太低)。当我尝试访问数据时出现问题。例如:
length(test$block) # length = 400, works
length(data[1]$block) # length = 0, doesn't work
为什么我使用索引无法访问有关数据框的信息,但是使用标准变量却没有问题?
编辑:
海峡(测试):
'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" ...
我认为你需要使用双括号表示法。 data[[1]]$block
应该有效
我有两种方法可以访问 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'))
两个输出看起来一样(我无法 post 图片,因为我的声誉太低)。当我尝试访问数据时出现问题。例如:
length(test$block) # length = 400, works
length(data[1]$block) # length = 0, doesn't work
为什么我使用索引无法访问有关数据框的信息,但是使用标准变量却没有问题?
编辑:
海峡(测试):
'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" ...
我认为你需要使用双括号表示法。 data[[1]]$block
应该有效