在lapply中deparse(substitute(x))

时间:2017-01-27 15:40:09

标签: r lapply

我正在尝试在包含数据框名称的多个数据框架中创建一个列。当他们被附加时就是这样,我知道他们来自哪里。对于每个数据帧,这很容易手动完成:

# create some example data that need joining
Pb = c(5, 6, 4, 5, 7)
depth = c(1, 2, 3, 4, 5)
df1 <- data.frame(Pb, depth)
df2 <- df1 - 0.5

# add a column to each with the name of the original dataframe
df1$label <- deparse( substitute(df1) )
df2$label <- deparse( substitute(df2) )

产生所需的输出:

> df1
  Pb depth label
1  5     1   df1
2  6     2   df1
3  4     3   df1
4  5     4   df1
5  7     5   df1
> df2
   Pb depth label
1 4.5   0.5   df2
2 5.5   1.5   df2
3 3.5   2.5   df2
4 4.5   3.5   df2
5 6.5   4.5   df2

我正在尝试创建一个接受数据帧列表并执行相同操作的函数,但我在deparse(substitute(x))中使用lapply时遇到了困难。我的(不工作)代码如下:

label_tool = function(list) {    
addlabels = function(x){ x$labels<-deparse(substitute(x)) }
list <- lapply( list, addlabels )   
return(df)
}

# example usage (see above)
list <- list( df1, df2 )
list <- label_tool(list)

我看到others已设法在deparse(substitute(x))中使用lapply,但坦率地说,我不知道如何使用<?php header('Content-Type: application/json'); $con3 = mysqli_connect("p:localhost", "root", "", ""); if (mysqli_connect_errno($con)) { echo "Failed to connect to DataBase: " . mysqli_connect_error(); } else { $data_points = array(); $result = mysqli_query($con, "SELECT ID,AccountID,time FROM tbhealth_temp order by time"); while ($row = mysqli_fetch_array($result)) { $point = array("value1" => $row['time'], "value2" => $row['TempValue']); array_push($data_points, $point); } echo json_encode($data_points); } mysqli_close($con); ?> 。有人可以帮忙吗?

0 个答案:

没有答案