2012-07-20 6 views
1

단순히 내 데이터 프레임을 내 열 intervention의 값을 기반으로 여러 데이터 프레임으로 분할하려하지만 시도 할 때 예상치 못한 결과가 나타납니다.이상한 결과를내는 데이터 프레임 분할

이 나는 ​​참으로 dataframe는 raw라는 이름의 한 점검과 :

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 

가 그럼 난

0을 사용

print(class(raw)); 

는 dataframe 분할하기 전에 여기

[1] "data.frame" 

를 얻을 수 있어요

$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의 목록처럼 보이지 않는 : 나는

print(groups); 

를 인쇄 할 때 1,236,311,549,

와 나는이 얻을.

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") 

편집 여기 내 전체 코드는, 그것은 작은 것 : 또한 이상한 라인을 (원시) dput의

$corral.corral.horseshoe.test_intervention.test_intervention 

편집

출력을 확인할 수 있습니다.

#!/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) '의 출력을 제공하십시오. –

답변

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 

당신 같은 얻고 있지. 당신이 목록의 dataframe를 생성하는 data.frame에 RJSON의 출력을 변환 할 때 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" 

는 raw1로하고, 당신의 dataframe이 잘

원료를 작동하는 것 같다

이 라인은 문제

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

사용하는 것입니다

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

당신이 여기에서했던 것처럼 텍스트를 복사하여 터미널에 붙여 넣을 때 나는 또한 이것을 얻습니다 ... – PandemoniumSyndicate

+0

오, 나는 raw1의 dput을보고 있습니다. 그래서 fromJson은 각각 하나의 변수가있는 데이터 프레임 목록을 제공합니다. as.data.frame (do.call (rbind, raw))은 적절한 데이터 프레임으로 변환하려고 시도하지만 이제 각 행이 목록 인 데이터 프레임이 있습니다. 적절한 데이터 프레임으로 포맷 할 수있는 방법이 있습니까? – PandemoniumSyndicate

+0

@PandemoniumSyndicate' 원시 <- 데이터.프레임 (lapply (raw, unlist))'은 현재 상태에서 적절한 데이터 프레임으로'raw'를 얻는 것처럼 보입니다. 그러나이 홀수 상태를 건너 뛰는 더 좋은 방법이있을 수 있습니다. –

관련 문제