2013-02-25 1 views
-1

lapply 내에서 data.frame의 값을 인쇄해도 그 값은 유지되지 않습니다.lapply 관련 문제

lapply(names(RFEresults), function(x) 
    { 
    feats <- extractFeatures(RFEresults[[x]]) 
    featurestat[which(featurestat[, 1]==x),rownames(feats)] <- feats$time.choosen 
    print(featurestat[1, ]) 
    }) 

print(featurestat[1, ]) 

lapply에는 값이 저장되지 않습니까?

+1

를? 당신이 얻는 결과는 무엇입니까? 수동으로 할 때 작동합니까? 참고 사항 :'RFEresults'를'lapply' 호출에 전달하는 것이 좋습니다. –

+0

@ RomanLuštrik 색인을 생성 할 때 이름을 사용해야합니다. print 함수 호출은 featurestat를 올바르게 인쇄하지만 lapply의 동일한 외부는 원본 data.frame을 인쇄합니다. featurestat는 0으로 초기화됩니다. – Shahzad

+0

함수 내에서 수정 된 @Shahzad 객체는 함수 환경에서 수정됩니다. 이 기능을 종료하면 환경이 동일하지 않으므로 변경 사항이 손실됩니다. 재현 가능한 예제를 제공하면 귀하의 코드를 도울 수 있습니다. – juba

답변

1

이 시도 :

#assign result of the lapply loop to an object 
res <- lapply(names(RFEresults), function(x) 
    { 
    feats <- extractFeatures(RFEresults[[x]]) 
    featurestat[which(featurestat[, 1]==x),rownames(feats)] <- feats$time.choosen 
    featurestat #return value of the function 
    }) 

#now you have the results of each iteration in a list 
#and can access them using 
print(res[[1]]) 
print(res[[2]]) 
#... 
0

이 정결 있는지 모르겠어요하지만이 작업을해야 문제가 무엇인지 정확하게

lapply(names(RFEresults), function(x) 
{ 
    feats <- extractFeatures(RFEresults[[x]]) 
    featurestat[which(featurestat[, 1]==x),rownames(feats)] <<- feats$time.choosen 
    print(featurestat[1, ]) 
})