2017-03-02 6 views
0

現在、あるデータフレームの行を別のマスタデータフレームに追加するループがあります。残念ながら、それは文字を数字に変換しますが、私はそれを望んでいません。文字を維持しながら、1つのデータフレームからマスターデータフレームに行を追加するには、次の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

関連する問題