问题
I am trying to add a new column to data frame using RCpp.
In the following codes, I intend to add a "result" column to dataframe, df. But the dataset does not have "result" column after running the codes. Could you tell me what is wrong with them?
R file to call AddNewCol() function.
library(Rcpp)
sourceCpp('AddNewCol.cpp')
AddNewCol( df ,"result")
AddNewCol.cpp
#include <Rcpp.h>
#include<math.h>
using namespace Rcpp;
// [[Rcpp::export]]
void AddNewCol(DataFrame& df, std::string new_var) {
int maxRow = df.nrows();
NumericVector vec_x = df["x"];
NumericVector vec_y = df["y"];
NumericVector resultvec = NumericVector(maxRow);
for( int i = 0 ; i < maxRow; i++ ){
resultvec[i] = vec_x[i] * pow( vec_y[i] , 2 );
}
df[new_var] = resultvec;
}
回答1:
You cannot do it by reference. But if you return the data frame it works:
#include <Rcpp.h>
using namespace Rcpp;
// [[Rcpp::export]]
DataFrame AddNewCol(const DataFrame& df, std::string new_var) {
NumericVector vec_x = df["x"];
NumericVector vec_y = df["y"];
df[new_var] = vec_x * Rcpp::pow(vec_y, 2);
return df;
}
/*** R
set.seed(42)
df <- data.frame(x = runif(10), y = runif(10))
AddNewCol( df ,"result")
*/
Note that I have taken the liberty to simplify the computation a bit. Result:
> set.seed(42)
> df <- data.frame(x = runif(10), y = runif(10))
> AddNewCol( df ,"result")
x y result
1 0.9148060 0.4577418 0.191677054
2 0.9370754 0.7191123 0.484582715
3 0.2861395 0.9346722 0.249974991
4 0.8304476 0.2554288 0.054181629
5 0.6417455 0.4622928 0.137150421
6 0.5190959 0.9400145 0.458687354
7 0.7365883 0.9782264 0.704861206
8 0.1346666 0.1174874 0.001858841
9 0.6569923 0.4749971 0.148232064
10 0.7050648 0.5603327 0.221371155
来源:https://stackoverflow.com/questions/51879038/how-can-i-add-a-new-column-to-dataframe-in-rcpp