我無法將某些R代碼的結果輸出到數據框中,以便最終導出爲CSV。我對R很新,所以我不確定究竟是什麼錯誤。將結果輸出到數據框中
我有這個輸入數據集,名爲 「ip_test」:
ip ip_end country_name region_name city_name
702388992 702388994 US West LA
我然後運行該代碼:
g = 1
ip = as.numeric(ip_test[g,1])
df <- for(g in 1:as.numeric(nrow(ip_test)))
{ for(ip in as.numeric(ip_test[g,1]):as.numeric(ip_test[g,2]))
{ print(cbind(ip_address=ip,
country_name = paste(ip_test[g,3], collapse=" "),
region_name = paste(ip_test[g,4], collapse=" "),
city_name = paste(ip_test[g,5], collapse=" ")))
}
}
這是輸出的結果:
ip_address country_name region_name city_name
[1,] "702388992" "US" "West" "LA"
ip_address country_name region_name city_name
[1,] "702388993" "US" "West" "LA"
ip_address country_name region_name city_name
[1,] "702388994" "US" "West" "LA"
我遇到的問題是數據幀「df」沒有被填充任何數據,它仍然是「NULL(空)」。由於輸出的數據是我想要的,我不確定問題是什麼。我研究了矩陣與數據幀問題,輸出爲ldply,as.data.frame,R list to data frame。在每一位代碼中,我都遇到了同樣的問題:數據幀沒有輸出。我嘗試了reshape2庫中的「熔化」功能,每Multidimensional array into data frame創建了一個數據幀1列寬11行,數字爲0-10,此錯誤: 「名稱(對象)中的錯誤< - nm: 'name'屬性1必須與向量[0]的長度相同「
我在想這是基本的輸出,但我無法弄清楚這個問題。任何幫助將非常感激。
隨着你計算的每一個新行,你都需要'將它'綁定到df。你能不能給它一個... – cephalopod
請顯示一些數據,包括* ip_test *對象。 – Parfait
@cephalopod rbind是要走的路,我只是不熟悉它。我沒有意識到「印刷」和「混搭」之間的區別。 –