1

我有一个x包含 8 个整数列(以及大约 1000 行数据)的数据框。我创建了一个 UDF“测试”,它接受 8 个整数参数并返回一个值。我通过传递任意整数值测试了 UDF,它确实返回了一个值,所以我知道它可以工作。我现在想将 8 个整数列逐行传递,并让它将值作为数据框中每一行的新列返回。我已经尝试过x$NewColumn = test(x$Col1, x$Col2 .... x$Col8),但该函数返回一个错误,表明数据未正确传递。有人可以告诉我我做错了什么吗?

4

3 回答 3

1

你可以使用mapply

mapply(test, x$Col1, x$Col2 .... x$Col8)
于 2013-04-30T16:18:34.957 回答
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
于 2013-04-30T16:22:40.463 回答
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
于 2013-04-30T16:08:16.143 回答