Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/67.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R扩展函数(选择了…未定义列中的错误)_R_Undefined_Spread - Fatal编程技术网

R扩展函数(选择了…未定义列中的错误)

R扩展函数(选择了…未定义列中的错误),r,undefined,spread,R,Undefined,Spread,我用谷歌搜索了我的错误,但这对我没有帮助 得到一个数据帧,带有一个列x unique(df$x) 结果是: [1] "fc_social_media" "fc_banners" "fc_nat_search" [4] "fc_direct" "fc_paid_search" 当我尝试这个: df <- spread(data = df, key = x, value = x, fill = "0

我用谷歌搜索了我的错误,但这对我没有帮助

得到一个数据帧,带有一个列x

unique(df$x) 
结果是:

[1] "fc_social_media"         "fc_banners"              "fc_nat_search"          
[4] "fc_direct"               "fc_paid_search"
当我尝试这个:

df <- spread(data = df, key = x, value = x, fill = "0") 
但这很奇怪,因为我在不同的时间使用了扩展函数(在同一个脚本中)

所以我在谷歌上看到了一些“解决方案”:

  • 我删除了所有的“特殊”字符。正如你所看到的,我的 值不包含特殊字符(已清除)。但事实并非如此 救命啊
  • 我检查了是否有同名的列。但是所有的列名 它们是独一无二的
@Gregor,@Akrun:

    > str(df)
'data.frame':   100 obs. of  22 variables:
 $ visitor_id             : chr  "321012312666671237877-461170125342559040419" "321012366667112237877-461121705342559040419" "321012366661271237877-461170534255901240419" "321012366612671237877-461170534212559040419" ...
 $ visit_num              : chr  "1" "1" "1" "1" ...
 $ ref_domain             : chr  "l.facebook.com" "X.co.uk" "x.co.uk" "" ...
 $ x                      : chr  "fc_social_media" "fc_social_media" "fc_social_media" "fc_social_media" ...
 $ va_closer_channel      : chr  "Social Media" "Social Media" "Social Media" "Social Media" ...
 $ row                    : int  1 2 3 4 5 6 7 8 9 10 ...
 $                        : chr  "0" "0" "0" "0" ...
 $ Hard Drive             : chr  "0" "0" "0" "0" ...

该错误可能是由于列没有名称造成的,即
”。使用可复制的示例

library(tidyr)
spread(df, x, x)
[.data.frame
中出错(数据,setdiff(名称(数据),c(键变量, 值(u var)):选择了未定义的列

我们可以通过更改列名使其工作

names(df) <- make.names(names(df))
spread(df, x, x, fill = "0")
#   X fc_banners fc_direct fc_nat_search fc_paid_search fc_social_media
#1 1          0         0             0              0 fc_social_media
#2 2 fc_banners         0             0              0               0
#3 3          0         0 fc_nat_search              0               0
#4 4          0 fc_direct             0              0               0
#5 5          0         0             0 fc_paid_search               0

names(df)发布
dput(droplevels(head(df))
怎么样?我们可以尝试查看您的数据结构发生了什么。(
$
使用部分匹配,因此
df$x
显示结果并不一定意味着您有一个名为
x
的列)“Akrun”;KEY=X和ValueX,它可以工作,但请将数据样本发布为@ GReGor建议。数据中是否有额外属性?您可以检查<代码> STR(DF)< /C> >对不起,您是对的。我更改了数据,现在您可以找到席找一个没有名字的列,即“代码>”“<代码>”。
names(df) <- make.names(names(df))
spread(df, x, x, fill = "0")
#   X fc_banners fc_direct fc_nat_search fc_paid_search fc_social_media
#1 1          0         0             0              0 fc_social_media
#2 2 fc_banners         0             0              0               0
#3 3          0         0 fc_nat_search              0               0
#4 4          0 fc_direct             0              0               0
#5 5          0         0             0 fc_paid_search               0
df <- data.frame(x =  c("fc_social_media",  "fc_banners", 
   "fc_nat_search", "fc_direct", "fc_paid_search"), x1 = 1:5, stringsAsFactors = FALSE)
names(df)[2] <- ""