Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/69.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/5/excel/28.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,如何对df中的每个值l应用以下函数rt。 x和y具有以下值 x<-9 y<-1 rt<-function(x,y,l) min(x,max(0,l-y)) df a b c 5 6 7 1 4 1 2 4 3 x如果您想继续使用数据帧,最简单的方法可能是使用apply并将MARGIN参数设置为c(1,2),这使得它可以通过行和列(即,对每个单元格)应用函数 x如果您想继续使用数据帧,最简单的方法可能是使用apply并将MARGIN参数设

如何对
df
中的每个值
l
应用以下函数
rt
x
y
具有以下值

 x<-9
 y<-1

rt<-function(x,y,l) min(x,max(0,l-y))

df
a   b   c
5   6   7
1   4   1
2   4   3

x如果您想继续使用数据帧,最简单的方法可能是使用
apply
并将
MARGIN
参数设置为
c(1,2)
,这使得它可以通过行和列(即,对每个单元格)应用函数


x如果您想继续使用数据帧,最简单的方法可能是使用
apply
并将
MARGIN
参数设置为
c(1,2)
,这使得它可以通过行和列(即,对每个单元格)应用函数


x
rt(x,y,unlist(df))
?你确定你真的想要
df
作为
数据。frame
而不是
矩阵
?是的,我想要它作为dataframe在你的
df
中什么是
x
y
l
l
都是
df
中的值<代码>x
rt(x,y,未列出(df))
?你确定你真的想要
df
作为
数据。frame
而不是
矩阵
?是的,我想要它作为dataframe在你的
df
中什么是
x
y
l
l
都是
df
中的值<代码>x
x <- 9
y <- 1

rt <- function(x, y, l) min(x, max(0, l-y))

df <- data.frame(a = c(5, 1, 2),
                 b = c(6, 4, 4),
                 c = c(7, 1, 3))

rt_df <- as.data.frame(apply(df, c(1,2), rt, x = x, y = y))