내가 4 열 데이터 프레임, 그 열 이름의 3을 포함하는 목록을 가지고 척이 목록 항목을 참조
#create data with 4 columns, a-d
a<-c(1,2,3)
b<-c(1,2,3)
c<-c(1,2,3)
d<-c(0.3,0.4,0.2)
data<-data.frame(a,b,c,d)
#create a list that doesnt include d
list<-c('a','b','c')
나는 루프를 실행하려면 여기서 한 번에 하나씩 열의 합계를 기반으로 값을 계산 한 다음이 정보를 나에게 작업 된 각 열의 ID와 계산 된 값을 제공하는 테이블로 저장합니다. 여기
내가 시도한 것입니다 :
#make output vectors for a loop
output.id<-c()
output.metric<-c()
#run loop
for(i in 1:length(list)){
#name of which id in the list you are working on
id<-list[i]
#compute something based on the data contained within a vector of the data frame, referencing where you are in the list
metric<- sum(data$list[i]*data$d)/sum(data$list[i])
#save the name of which id you were working on and the computed value for each element i
output.id<-c(output.id,id)
output.metric<-(output.metric,metric)
}
문제가 메트릭의 계산에 있습니다. 내가 작업하고있는 목록 항목 'i'에 따라 데이터 열을 호출하려고합니다. 그래서, 나는, 내가
metric<- sum(data$list[i]*data$d)/sum(data$list[i])
'는'
로 대체
metric<- sum(data$a*data$d)/sum(data$a)
곳 '목록 [i]를'로 해석 할 = 때를하는 좋은 방법이 있나요 이 작업을 수행?
당신이'list'을 변경하는 경우는,'data'는 당신이
list
및data
기능을 덮어 쓰기하지 않는, 그래서 나는 당신의 변수 이름을 변경했습니다 또한 기본 R 함수. – thelatemail감사 메일 - 감사합니다. – josliber