split()中的错误1:dim(X)[along]中的错误:NA / NaN参数

时间:2018-12-28 02:07:05

标签: r

我正在尝试使用baseR中的split()函数根据HUC4列将此数据帧拆分为多个数据帧。我一直遇到错误,导致该功能无法正常工作。我尚未在线找到解决方案。

   Saris/Palis       SznYr HUC4
1        1100500 2003-Winter  202
2        1100500   2017-Fall  202
3        1100500 2017-Summer  202
4        1100525 2006-Winter  202
5        1100525 2016-Winter  202
1200     5131700 2012-Summer  109
1201     5131700   2015-Fall  109
1202     5131700 2015-Spring  109
1203     5131700 2016-Winter  109
1204     5131775 2007-Spring  109
1205     5131780 2006-Winter  109
800      3522425 2007-Spring  108
801      3522450 2006-Spring  108
802      3522450   2007-Fall  108
803      3522450 2007-Summer  108
804      3522450 2011-Summer  108
805      3522450 2012-Spring  108

split(df,df$HUC4)

Error in 1:dim(X)[along] : NA/NaN argument
In addition: Warning messages:
1: In if (along < 0) along = length(dim(X)) - along + 1 :
  the condition has length > 1 and only the first element will be used
2: In 1:dim(X)[along] :
  numerical expression has 17 elements: only the first used

1 个答案:

答案 0 :(得分:1)

以下代码实际上有效:

df <- read.table(header = T, text = " Saris/Palis       SznYr HUC4
1        1100500 2003-Winter  202
2        1100500   2017-Fall  202
3        1100500 2017-Summer  202
4        1100525 2006-Winter  202
5        1100525 2016-Winter  202
1200     5131700 2012-Summer  109
1201     5131700   2015-Fall  109
1202     5131700 2015-Spring  109
1203     5131700 2016-Winter  109
1204     5131775 2007-Spring  109
1205     5131780 2006-Winter  109
800      3522425 2007-Spring  108
801      3522450 2006-Spring  108
802      3522450   2007-Fall  108
803      3522450 2007-Summer  108
804      3522450 2011-Summer  108
805      3522450 2012-Spring  108")

split(df, df$HUC4)

# $`108`
# Saris.Palis       SznYr HUC4
# 800     3522425 2007-Spring  108
# 801     3522450 2006-Spring  108
# 802     3522450   2007-Fall  108
# 803     3522450 2007-Summer  108
# 804     3522450 2011-Summer  108
# 805     3522450 2012-Spring  108
# 
# $`109`
# Saris.Palis       SznYr HUC4
# 1200     5131700 2012-Summer  109
# 1201     5131700   2015-Fall  109
# 1202     5131700 2015-Spring  109
# 1203     5131700 2016-Winter  109
# 1204     5131775 2007-Spring  109
# 1205     5131780 2006-Winter  109
# 
# $`202`
# Saris.Palis       SznYr HUC4
# 1     1100500 2003-Winter  202
# 2     1100500   2017-Fall  202
# 3     1100500 2017-Summer  202
# 4     1100525 2006-Winter  202
# 5     1100525 2016-Winter  202

所以我的猜测是您使用的数据帧存在结构性问题。

您可以发布str(df)吗?