Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ssh/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/asp.net-mvc-3/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
遍历R中的列表行_R - Fatal编程技术网

遍历R中的列表行

遍历R中的列表行,r,R,我在R中工作,希望遍历列表中的行,并按名称引用列值,如下所示: for(row in Data) { name <- row$name age <- row$age #do something cool here } name, age, gender, weight Bill, 23, m, 134 Carl, 40, m, 178 我知道这应该是微不足道的,但我找不到帮助。提前谢谢 这是我正在处理的原始数据。前面的表格就是一个例子: structure

我在R中工作,希望遍历列表中的行,并按名称引用列值,如下所示:

for(row in Data) {
    name <- row$name
    age <- row$age
    #do something cool here
}
name, age, gender, weight
Bill, 23, m, 134
Carl, 40, m, 178
我知道这应该是微不足道的,但我找不到帮助。提前谢谢

这是我正在处理的原始数据。前面的表格就是一个例子:

structure(list(startingTemp = c(100L, 100L, 100L, 100L, 100L), 
    endingTemp = c(1L, 1L, 1L, 1L, 1L), movesPerStep = c(200000L, 
    100000L, 20000L, 10000L, 2000L), coolingCoefficient = c(0.99, 
    0.99, 0.99, 0.99, 0.99), numberTempSteps = c(459L, 459L, 
    459L, 459L, 459L), costPerRun = c(91800000L, 45900000L, 9180000L, 
    4590000L, 918000L)), .Names = c("startingTemp", "endingTemp", 
"movesPerStep", "coolingCoefficient", "numberTempSteps", "costPerRun"
), row.names = c(NA, 5L), class = "data.frame")

您可以使用
apply
执行此操作:

apply(Data, 1, function(row) {
    name <- row["name"]
    age <- row["age"]
    #do something cool here
})

(注意,对于本例,您也可以只执行
Data$numberTempSteps/Data$costPerRun
)。

您看过
apply
<代码>应用(数据,1,函数)按行执行“酷事”。(将“1”更改为“2”可以按列进行操作。)这看起来不像列表:你是指数据帧吗?当我使用typeof()函数时,R表示它是一个列表。我是否需要将其转换以使其可行?能否请您执行
dput(Data)
并显示结果是什么?顺便说一句,应用于数据框的
typeof
确实给出了列表(基于它们的定义内部)。如果执行
is.data.frame(data)
操作,它将返回
TRUE
。谢谢!我知道一旦我有了正确的眼光,这将是微不足道的。
apply(Data, 1, function(row) row["numberTempSteps"] / row["costPerRun"])