5

从此数据框开始

myDF = structure(list(Value = c(-2, -1, 0, 1, 2)), .Names = "Value", row.names = c(NA, 5L), class = "data.frame")

假设我想在 myDF$Value 的每一行上运行这个函数

getNumberInfo <- function(x) {
if(x %% 2 ==0) evenness = "Even" else evenness="Odd"
if(x > 0) positivity = "Positive" else positivity = "NonPositive"
if (positivity == "Positive") logX = log(x) else logX=NA
c(evenness,positivity,logX)
} 

...获取此数据框

structure(list(Value = c(-2, -1, 0, 1, 2), Evenness = c("Even", 
"Odd", "Even", "Odd", "Even"), Positivity = c("NonPositive", 
"NonPositive", "NonPositive", "Positive", "Positive"), Log = c(NA, 
NA, NA, "0", "0.693147180559945")), row.names = c(NA, 5L), .Names = c("Value", 
"Evenness", "Positivity", "Log"), class = "data.frame")
4

3 回答 3

8

您可能希望更改getNumberInfo函数以返回列表而不是向量,以便值可以具有不同的类型。事实上,它们都被转换成字符串,这可能不是你想要的logX

getNumberInfo <- function(x) {
  if(x %% 2 ==0) evenness = "Even" else evenness="Odd"
  if(x > 0) positivity = "Positive" else positivity = "NonPositive"
  if (positivity == "Positive") logX = log(x) else logX=NA
  list(evenness,positivity,logX)
}

此外,您可以使用名称以获得更好的效果,这样您就不必重复它们:

getNumberInfo <- function(x) {
  list(evenness = if(x %% 2 ==0) "Even" else "Odd",
       positivity = if(x > 0) "Positive" else "NonPositive",
       logX = if(x > 0) log(x) else NA)
}

那么解决方案就变得简单了:

> cbind(myDF, t(sapply(myDF$Value, getNumberInfo)))
  Value evenness  positivity      logX
1    -2     Even NonPositive        NA
2    -1      Odd NonPositive        NA
3     0     Even NonPositive        NA
4     1      Odd    Positive         0
5     2     Even    Positive 0.6931472

最后,如果您使用ifelse(可以处理向量)而不是if,它会变得更加简单,因为您不必调用apply

getNumberInfo <- function(x) {
  list(evenness = ifelse(x %% 2 ==0, "Even", "Odd"),
       positivity = ifelse(x > 0, "Positive", "NonPositive"),
       logX = ifelse(x > 0, log(x), NA))
}

> cbind(myDF, getNumberInfo(myDF$Value))
  Value evenness  positivity      logX
1    -2     Even NonPositive        NA
2    -1      Odd NonPositive        NA
3     0     Even NonPositive        NA
4     1      Odd    Positive 0.0000000
5     2     Even    Positive 0.6931472

最后一个解决方案会发出警告,因为它实际上是在计算每个元素的日志,而不仅仅是那些带有x>0. 不确定最优雅的处理方式。

于 2009-09-08T19:00:13.650 回答
3

怎么样:

 out <- cbind(myDF, t(apply(myDF, 1, getNumberInfo)))
 colnames(out) <- c('Value', 'Evenness', 'Positivity', 'Log')

这给了你:

  值均匀度阳性日志
1 -2 偶数 非正数 NA
2 -1 奇数非正数 NA
3 0 偶数 非正数 NA
4 1 奇正 0
5 2 偶正数 0.693147180559945

于 2009-09-08T17:38:51.413 回答
3

另一种选择:

> library(plyr)
> df <- mdply(myDF, getNumberInfo)
> names(df) <- c('Value', 'Evenness', 'Positivity', 'Log')
> df
  Value Evenness  Positivity       Log
1    -2     Even NonPositive        NA
2    -1      Odd NonPositive        NA
3     0     Even NonPositive        NA
4     1      Odd    Positive 0.0000000
5     2     Even    Positive 0.6931472
于 2009-09-09T20:47:10.467 回答