2012-07-20 74 views
1

我只是试图根据列的值intervention将我的数据框分割成几个数据框,但是当我尝试执行此操作时,出现了一些意外输出。分裂数据框给出奇怪的输出

正在检查我确实有一个数据帧命名raw

print(class(raw)); 

产生

[1] "data.frame" 

这里的拆分前的数据框:

position         id equation  intervention 
1  -2 D9E4262D-5B6D-ADB8-D605-B97D63437064  9,5   corral 
2  1 B2FFB0B0-210E-022F-293A-0ABFDDB3DC4B  2,3   corral 
3  1 85905A69-50F7-AF73-7A51-08B8FDCFAF2D  1,2   horseshoe 
4  -2 76A55530-5A39-6A73-3216-D276EABFA2F6  3,4 test_intervention 
5  -1 4CFA5D1B-EA32-8584-A1C9-540D9FFB24CB  3,4 test_intervention 

然后我用

groups <- split(raw, raw$intervention); 

,当我打印

print(groups); 

我得到这个:

$corral.corral.horseshoe.test_intervention.test_intervention 
    position         id equation  intervention 
1  -2 D9E4262D-5B6D-ADB8-D605-B97D63437064  9,5   corral 
2  1 B2FFB0B0-210E-022F-293A-0ABFDDB3DC4B  2,3   corral 
3  1 85905A69-50F7-AF73-7A51-08B8FDCFAF2D  1,2   horseshoe 
4  -2 76A55530-5A39-6A73-3216-D276EABFA2F6  3,4 test_intervention 
5  -1 4CFA5D1B-EA32-8584-A1C9-540D9FFB24CB  3,4 test_intervention 

这看起来并不像通过干预分组dataframes的列表。还要注意的怪行

$corral.corral.horseshoe.test_intervention.test_intervention 

编辑

输出dput(生)的:

structure(list(position = list(-2, 1, 1, -2, -1), id = list("D9E4262D-5B6D-ADB8-D605-B97D63437064", 
    "B2FFB0B0-210E-022F-293A-0ABFDDB3DC4B", "85905A69-50F7-AF73-7A51-08B8FDCFAF2D", 
    "76A55530-5A39-6A73-3216-D276EABFA2F6", "4CFA5D1B-EA32-8584-A1C9-540D9FFB24CB"), 
    equation = list("9,5", "2,3", "1,2", "3,4", "3,4"), intervention = list(
     "corral", "corral", "horseshoe", "test_intervention", 
     "test_intervention")), .Names = c("position", "id", "equation", 
"intervention"), row.names = c(NA, -5L), class = "data.frame") 

编辑 这里是我完整的代码,它的小。

#!/usr/local/bin/Rscript --slave 
require("rjson", quietly=TRUE); 

# First we need to grab the items from the R api, and save them into a data frame 
raw = fromJSON(file="http://some/url.com"); 

#reformats data into dataframe 
raw <- as.data.frame(do.call(rbind,raw)); 
#we need to create a new dataframe formatted according to the needs of catR 
groups <- split(raw, raw$intervention); 
print(groups); 
#saveRDS(object=fromJSON(file="http://some/url.com"),file="/home/bitnami/IRT_data/core_standard.rda"); 

我跑我的代码如下所示:

~/Rscript my_R_file.R 
+0

我无法复制这个。请提供'dput(raw)'的输出。 – 2012-07-20 15:52:48

回答

2
raw1<-read.table(header=T,text=" position         id equation  intervention 
1  -2 D9E4262D-5B6D-ADB8-D605-B97D63437064  9,5   corral 
2  1 B2FFB0B0-210E-022F-293A-0ABFDDB3DC4B  2,3   corral 
3  1 85905A69-50F7-AF73-7A51-08B8FDCFAF2D  1,2   horseshoe 
4  -2 76A55530-5A39-6A73-3216-D276EABFA2F6  3,4 test_intervention 
5  -1 4CFA5D1B-EA32-8584-A1C9-540D9FFB24CB  3,4 test_intervention") 

groups <- split(raw1, raw1$intervention) 

> groups 
$corral 
    position         id equation intervention 
1  -2 D9E4262D-5B6D-ADB8-D605-B97D63437064  9,5  corral 
2  1 B2FFB0B0-210E-022F-293A-0ABFDDB3DC4B  2,3  corral 

$horseshoe 
    position         id equation intervention 
3  1 85905A69-50F7-AF73-7A51-08B8FDCFAF2D  1,2 horseshoe 

$test_intervention 
    position         id equation  intervention 
4  -2 76A55530-5A39-6A73-3216-D276EABFA2F6  3,4 test_intervention 
5  -1 4CFA5D1B-EA32-8584-A1C9-540D9FFB24CB  3,4 test_intervention 

没有得到你一样。似乎做工精细

生是你的数据帧,RAW1是版本的函数read.table

> str(raw1$intervention) 
Factor w/ 3 levels "corral","horseshoe",..: 1 1 2 3 3 
> str(raw$intervention) 
List of 5 
$ : chr "corral" 
$ : chr "corral" 
$ : chr "horseshoe" 
$ : chr "test_intervention" 
$ : chr "test_intervention" 

当您转换输出从RJSON它是创建列表的数据帧一data.frame

这条线是你的问题

#reformats data into dataframe 
raw <- as.data.frame(do.call(rbind,raw)); 

使用

raw2<-data.frame(lapply(raw,unlist)) 
+0

我也会得到这个,当我只是复制并粘贴文本到终端,因为你在这里完成... – PandemoniumSyndicate 2012-07-20 16:00:22

+0

哦,我看到了,看着raw1的dput。所以来自Json给了我一个数据框的列表,每个数据框都有一个变量。 as.data.frame(do.call(rbind,raw)试图把它变成一个合适的数据帧,但是现在我有一个数据帧,每一行都是一个列表。是否有一种方法可以将它格式化为一个合适的数据帧? – PandemoniumSyndicate 2012-07-20 16:15:59

+0

@PandemoniumSyndicate'raw < - 数据。框架(lapply(raw,unlist))'看起来像将它从当前状态变成适当的数据框。但是可能有更好的方式完全跳过这种奇怪的状态。 – 2012-07-20 16:19:56