本文介绍了将循环应用于数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将Johnson包中的函数RE.Johnson应用到整个数据帧df,其中包含对16个变量的157个观察值,我想循环遍历所有数据帧而不是这样做手动.我已经尝试了以下代码,但无法正常工作.

I'm trying to apply the function RE.Johnson from the Johnson package to a whole data frame df that contains 157 observations of 16 variables and i'd like to loop trough all the dataframe instead of doing it manually.I've tried the following code but it doesn't work.

lapply(df[1:16], function(x) RE.Johnson(x))

我知道这对你们来说似乎很容易,但是我只是从R开始.谢谢

I know it might seem easy for you guys but I'm juste starting with R.Thanks

编辑

R为我提供了答案Error in RE.ADT(xsl[, i]) : object 'p' not found,并且数据未转换.这是数据摘要:

R provides me the answer Error in RE.ADT(xsl[, i]) : object 'p' not found and the data are not transformed.And here is a summary of the data:

data.frame':    157 obs. of  16 variables:
$ X         : num  786988 781045 777589 775266 786843 ...
$ Y         : num  486608 488691 490089 489293 488068 ...
$ Z         : num  182 128 191 80 131 ...
$ pH        : num  7.93 7.69 7.49 7.66 7.92 7.08 7.24 7.19 7.44 7.37 ...
$ CE        : num  0.775 3.284 3.745 4.072 0.95 ...
$ Nitrate   : int  21 14 18 83 30 42 47 101 85 15 ...
$ NP        : num  19.6 43.6 31.7 18.6 31.7 ...
$ Cl        : num  1.9 21.3 2.56 21.5 3.2 ...
$ HCO3      : num  6.65 4.85 4.4 7.72 4.1 ...
$ CO3       : num  0 0 0 0 0.0736 ...
$ Ca        : num  4.12 7.52 3.48 7.58 4.8 10 4.4 4.6 4.2 7.4 ...
$ Mg        : num  3.94 8.92 2.34 7.1 2.5 ...
$ K         : num  0.1442 0.0759 0.0709 0.3691 0.07 ...
$ Na        : num  2.41 34.55 2.51 44.01 2.1 ...
$ SO4       : num  1.45 23.6 1.2 26.66 2 ...
$ Residu_sec: num  0.496 2.102 2.397 2.606 0.608 ...

推荐答案

这不是一个完整的解决方案,只是一些其他信息.

Not a complete solution, just some information for others.

我在iris数据框中的列上手动尝试了Johnson::RE.Johnson.似乎仅对于Sepal.LengthPetal.Length可以正常工作:

I tried the Johnson::RE.Johnson manually on the columns in the iris data frame. It seems to be work fine for Sepal.Length and Petal.Length only:

lapply(iris[c(1,3)], Johnson::RE.Johnson)

...,它返回您为Sepal.WidthPetal.Width提及的错误.

... and it returns the error you mentioned for Sepal.Width and Petal.Width.

lapply(iris[c(2,4)], Johnson::RE.Johnson)

Error in RE.ADT(xsl[, i]) : object 'p' not found

这似乎很奇怪,因为所有这些列的数据类型均为num. iris数据框似乎没有任何缺失值或多余的字符值隐藏在任何地方,因此我不确定为什么该计算适用于那些列而不适用于其他列.

This seems odd because all of those columns have a data type of num. The iris data frame doesn't appear to have any missing values or extra character values hidden anywhere, so I'm not sure why the calculation is working for those columns but not others.

Johnson::RE.Johnson对数据的处理了解得太多,它似乎无法计算p的值,并且无法完成这些列的迭代.

Without understanding too much about what the Johnson::RE.Johnson is doing to the data, it looks like it is unable to calculate a value for p and is unable to complete the iteration for those columns.

从探索源代码开始,此功能似乎在此时崩溃了:

From exploring the source code, the function appears to break down at this point:

  if (xsb.valida[1, i] == 0)
    xsb.adtest[1, i] <- (Johnson::RE.ADT(xsb[, i])$p) # succeeds
  if (xsl.valida[1, i] == 0)
    xsl.adtest[1, i] <- (Johnson::RE.ADT(xsl[, i])$p) # fails
  if (xsu.valida[1, i] == 0)
    xsu.adtest[1, i] <- (Johnson::RE.ADT(xsu[, i])$p) # fails

该函数尝试在xsl上运行Johnson::RE.ADT,xsl此时仅是0的向量. RE.ADT返回相同的错误,但找不到p值.

The function attempts to run Johnson::RE.ADT on xsl, which at this point is a vector of just 0's. The RE.ADT returns the same error with the p value not being found.

这篇关于将循环应用于数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 10:23
查看更多