0

我无法遍历 data.Frame。

# a is a dataframe, with names 'f','d','sess', ...
for (x in a) 
# find all events BEFORE the event x 
#    ('d' is the beginning of the event in ms, 'e' is the end of it)
a[a$f < as.numeric(x['d']),] -> all_before

x['Epe'] = min( as.numeric(x['d'])-all_before$f)
}

它只是不会改变我原来的data.frame。有没有办法 即时更改我的数据框,或者我应该绝对创建一个新的并填写它?谢谢!

4

1 回答 1

0

数据框本质上是一个列表。使用数据框作为 for 循环的输入与使用列表作为输入时的作用相同。

> for(x in list(x = 1:3, y = letters, z = lm(mpg ~ hp, data = mtcars))){print(x)}
[1] 1 2 3
 [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s"
[20] "t" "u" "v" "w" "x" "y" "z"

Call:
lm(formula = mpg ~ hp, data = mtcars)

Coefficients:
(Intercept)           hp  
   30.09886     -0.06823  

它遍历列表的每个元素。在数据帧的情况下,这意味着它遍历数据帧的列。

> dat <- data.frame(x = 1:3, y = rep("this is y", 3))
> dat
  x         y
1 1 this is y
2 2 this is y
3 3 this is y
> for(i in dat){print(i)}
[1] 1 2 3
[1] this is y this is y this is y
Levels: this is y
于 2013-04-23T19:26:36.460 回答