向 xdf 文件添加新列
Adding a new column to an xdf file
我想使用 rxDataStep
向 xdf 文件添加一个新列。为了达到这个目的,我写了这段代码:
rxDataStep(nyc_jan_xdf,transformFunc = CashVsCard )
CashVsCard<-function(dataList)
{
if(dataList$payment_type==1)
{
dataList$cash_vs_Card="Card"
}
else
{
if(dataList$payment_type==2)
{
dataList$cash_vs_Card="Cash"
}
}
return(dataList)
}
然后我得到这个错误:
The variable 'cash_vs_Card' has a different number of rows than other columns in the data: 1 vs. 10
Caught exception in file: CxAnalysis.cpp, line: 3830. ThreadID: 7288 Rethrowing.
Caught exception in file: CxAnalysis.cpp, line: 5347. ThreadID: 7288 Rethrowing.
Error in doTryCatch(return(expr), name, parentenv, handler) :
The variable 'cash_vs_Card' has a different number of rows than other columns in the data: 1 vs. 10
In addition: Warning messages:
1: In if (dataList$payment_type == 1) { :
the condition has length > 1 and only the first element will be used
2: In if (dataList$payment_type == 2) { :
the condition has length > 1 and only the first element will be used
使用 ifelse
基于另一个变量的值进行矢量化转换。
cashVsCard <- function(datalist)
{
datalist$cash_vs_card <- ifelse(datalist$payment_type == 1, "Card", "Cash")
datalist
}
rxDataStep(*, transformFunc=cashVsCard)
我想使用 rxDataStep
向 xdf 文件添加一个新列。为了达到这个目的,我写了这段代码:
rxDataStep(nyc_jan_xdf,transformFunc = CashVsCard )
CashVsCard<-function(dataList)
{
if(dataList$payment_type==1)
{
dataList$cash_vs_Card="Card"
}
else
{
if(dataList$payment_type==2)
{
dataList$cash_vs_Card="Cash"
}
}
return(dataList)
}
然后我得到这个错误:
The variable 'cash_vs_Card' has a different number of rows than other columns in the data: 1 vs. 10
Caught exception in file: CxAnalysis.cpp, line: 3830. ThreadID: 7288 Rethrowing.
Caught exception in file: CxAnalysis.cpp, line: 5347. ThreadID: 7288 Rethrowing.
Error in doTryCatch(return(expr), name, parentenv, handler) :
The variable 'cash_vs_Card' has a different number of rows than other columns in the data: 1 vs. 10
In addition: Warning messages:
1: In if (dataList$payment_type == 1) { :
the condition has length > 1 and only the first element will be used
2: In if (dataList$payment_type == 2) { :
the condition has length > 1 and only the first element will be used
使用 ifelse
基于另一个变量的值进行矢量化转换。
cashVsCard <- function(datalist)
{
datalist$cash_vs_card <- ifelse(datalist$payment_type == 1, "Card", "Cash")
datalist
}
rxDataStep(*, transformFunc=cashVsCard)