我正在嘗試創建一個新的數據幀列,並在for循環內爲其賦值。分配錯誤 - 第一個參數無效
具體來說,我有一個數據框名稱的列表,我正在逐一循環這個列表。對於每個名稱,我想爲該數據框創建一個列$location
,我想爲其分配一個值。爲了簡單起見,假設我想分配它NA
。下面的代碼解釋了我想要做的事情。然而,當我運行它,我收到錯誤Error in assign(get(name)$location, NA) : invalid first argument
names<-c("europe", "africa", "asia")
# where each of the strings in `names` is the name of an existing data frame object
# for sake of example, code is provided below to create example data frame objects
x <- c(2,6,7)
y <- c(40, 42, 14)
europe <- data.frame(x,y)
africa <- data.frame(x,y)
asia <- data.frame(x,y)
for (name in names) {
assign(get(name)$location, NA)
}
我的目標是獲得一個叫我的每個數據幀的位置新列。我不知道如何解決這個問題或者爲什麼會發生。任何幫助將不勝感激。
數據框已經在我的工作區中有價值了......我留下了名字以顯示我正在嘗試做什麼。我將編輯我的問題以添加代碼來創建歐洲數據框。 – whistler
我編輯了我的問題,我希望現在回答這個問題。 – Llopis