2017-03-02 4 views
0

현재 하나의 데이터 프레임에서 다른 마스터 데이터 프레임으로 행을 추가하는 루프가 있습니다. 불행히도, 그것은 문자를 숫자로 변환하지만, 나는 그것을 원하지 않습니다. 문자를 유지하면서 한 데이터 프레임의 행을 마스터 데이터 프레임에 추가하려면 다음 for 루프를 어떻게 얻을 수 있습니까?For 루프의 문자로 데이터 프레임 채우기 R

AnnotationsD <- data.frame(x = vector(mode = "numeric", 
    length = length(x)), type = 0, label = 0, lesion = 0) 

x = c(1,2) 

for(i in length(x)){ 
D = data.frame(x = i, type = c("Distance"), 
    label = c("*"), lesion = c("Wild")) 

AnnotationsD[[i,]] <- D[[i]] 
} 

그래서 나는이 나올 싶은 것이 있습니다 :

x type  label lesion 
1 1 Distance *  Wild 
2 2 Distance *  Wild 

답변

1

이 작동합니다 :

x = c(1,2) 
AnnotationsD <- data.frame(x = as.character(NA), type = as.character(NA), 
          label = as.character(NA), lesion = as.character(NA), 
          stringsAsFactors =F) 

for(i in 1:length(x)){ 
      D = c(x = as.character(i), type = as.character("Distance"), 
      label = as.character("*"), lesion = as.character("Wild")) 

      AnnotationsD[i,] <- D 
      } 
+0

신난다! 고맙습니다. user3640617! – albeit2