2017-09-06 1 views
1

나는 목록 "cluster_features"가, 여기처럼 보이는 무엇과 :데이터 프레임에 대한 목록을 작성 후 CSV하지만 2 열

dput(cluster_features[1:3]) 
structure(list(cluster1 = structure(c(281673, 10759, 10756, 10756, 
10756, 2917), .Names = c("dui", "control", "physic", "dui_physic", 
"physic_control", "alcohol")), cluster2 = structure(c(41964, 
17445, 17445, 2815, 2815, 920, 920, 580, 503), .Names = c("investig", 
"auto", "investig_auto", "death", "death_investig", "non-crim", 
"investig_non-crim", "fire", "fire_investig")), cluster3 = structure(c(98605, 
29118, 18968, 16655, 16107, 10261, 8354, 7047, 6952, 6105, 5679, 
5642, 5630, 5306, 5303, 5259, 4965, 4954, 4954, 4954), .Names = c("assist", 
"agenc", "agenc_assist", "assist_other", "public", "motorist", 
"motorist_assist", "fire", "assist_public", "servic", "public_assist", 
"call", "general", "general_assist", "servic_call", "cad", "assist_fire", 
"escort", "assist_escort", "escort_servic"))), .Names = c("cluster1", 
"cluster2", "cluster3")) 

내 목표는 다음과 같습니다에 의해 생성 된

> cluster_features[1:3] 
$cluster1 
      dui  control   physic  dui_physic physic_control  alcohol 
     281673   10759   10756   10756   10756   2917 

$cluster2 
     investig    auto  investig_auto    death death_investig   non-crim 
      41964    17445    17445    2815    2815    920 
investig_non-crim    fire  fire_investig 
       920    580    503 

$cluster3 
     assist   agenc agenc_assist assist_other   public  motorist motorist_assist 
      98605   29118   18968   16655   16107   10261   8354 
      fire assist_public   servic public_assist   call   general general_assist 
      7047   6952   6105   5679   5642   5630   5306 
    servic_call    cad  assist_fire   escort assist_escort escort_servic 
      5303   5259   4965   4954   4954   4954 

유사한 형식의 데이터를 사용하여 CSV로 내보낼 수 있습니다. 즉, 아마도 columnA는 키이고 그 다음 키는 모든 키 값입니다. 예 : 이 : enter image description here

나는 노력이 :

x <- do.call("rbind", lapply(cluster_features, as.data.frame)) 

그러나 하나의 열에 결과 장소 다 그래서 같은 :

head(x) 
         X[[i]] 
cluster1.dui   281673 
cluster1.control   10759 
cluster1.physic   10756 
cluster1.dui_physic  10756 
cluster1.physic_control 10756 
cluster1.alcohol   2917 

에서 CSV로 내 목록을 내보낼 수있는 방법이 있나요 형식의 스크린 샷? 한 열의 키는 다른 열의 값입니까?

답변

1

올바르게 이해하면 이름이 지정된 벡터 목록 &을 가지고 각 벡터를 값이 두 개인 열 데이터 프레임 &으로 변환해야합니다. 이 시도 : 당신이 소스를 구별 할 필요가없는 경우, 당신은 데이터 프레임의 첫 번째 열을 건너 뛸 수 있습니다

result <- lapply(seq_along(cluster_features), 
       function(i){data.frame(source = paste0("cluster", i), 
             key = names(cf[[i]]), 
             value = cf[[i]])}) %>% 
    data.table::rbindlist() 

> head(result) 
    source   key value 
1: cluster1   dui 281673 
2: cluster1  control 10759 
3: cluster1   physic 10756 
4: cluster1  dui_physic 10756 
5: cluster1 physic_control 10756 
6: cluster1  alcohol 2917 

.

+0

네, 고맙습니다. –

관련 문제