我有一个数据框x
,包含8个整数列(以及大约1000行数据)。我创建了一个UDF'测试'它需要8个整数参数并返回一个值。我通过传递任意整数值来测试UDF,它确实返回一个值,所以我知道它有效。我想现在逐行传递8个整数列,并让它作为数据框中每一行的新列返回值。我尝试了x$NewColumn = test(x$Col1, x$Col2 .... x$Col8)
,但该函数返回一个错误,表明数据未正确传递。有人能告诉我我做错了吗?
答案 0 :(得分:1)
您可以使用mapply
mapply(test, x$Col1, x$Col2 .... x$Col8)
答案 1 :(得分:1)
df = data.frame(matrix(runif(80),ncol=8))
# creation of a matrix for the example
my.function = function (x) { return (mean(x)) } # write your function
# and then use the apply function
new.column = apply(df,1, my.function)
df$new.column = new.column
答案 2 :(得分:0)
尝试使用apply
函数在data.frame的行中运行:
## Create some data
df <- as.data.frame( matrix(runif(40),10) )
## Now we can use 'apply'. The '1' in the second argument means we apply across rows, if it were two we would apply across columns.
## The function we are applying to each row is to sum all the values in that row
df$Total <- apply( df , 1 , sum )
## We can also pass 'anonymous' functions. In this instance our function takes a single vector, 'x'
## 'x' is all the values of that row, and we can use them like so to do the same thing as 'sum' in the previous example
df$Function <- apply( df , 1 , function(x) x[1] + x[2] + x[3] + x[4] )
## And if we see what is in df, 'df$Total' and 'df$Function' should have the same values
df
# V1 V2 V3 V4 Total Function
#1 0.6615353 0.5900620 0.02655674 0.1036002 1.381754 1.381754
#2 0.8471900 0.8927228 0.77014101 0.6379024 3.147956 3.147956
#3 0.8783624 0.6769206 0.09598907 0.6681616 2.319434 2.319434
#4 0.7845933 0.8992605 0.13271067 0.3691835 2.185748 2.185748
#5 0.9753706 0.1374564 0.12631014 0.3693808 1.608518 1.608518
#6 0.4229039 0.7590963 0.79936058 0.2674258 2.248787 2.248787
#7 0.2635403 0.6454591 0.98748926 0.5888263 2.485315 2.485315
#8 0.7008617 0.7505975 0.39355439 0.5943362 2.439350 2.439350
#9 0.1169755 0.1961099 0.88216054 0.3383819 1.533628 1.533628
#10 0.3298974 0.0110522 0.88460835 0.3700531 1.595611 1.595611