簡體   English   中英

使用功能更新數據框並應用

[英]Updating a dataframe with function and sapply

我試圖根據國家/地區在數據框中設置等於“美國”或“外國”的列。 我相信這樣做的正確方法是編寫一個函數,然后使用sapply實際更新數據幀。 這是我第一次在R -in SQL嘗試這樣的操作,我只會編寫UPDATE查詢。

這是我的數據框:

str(clients)
'data.frame':   252774 obs. of  4 variables:
 $ ClientID     : Factor w/ 252774 levels "58187855","59210128",..: 19 20 21 22 23 24 25 26 27 28 ...
 $ Country          : Factor w/ 207 levels "Afghanistan",..: 196 60 139 196 196 40 40 196 196 196 ...
 $ CountryType     : chr  "" "" "" "" ...
 $ OrderSize        : num  12.95 21.99 5.00 7.50 44.5 ...


head(clients)
       ClientID  Country       CountryType  OrderSize
1      58187855  United States              12.95
2      59210128  France                     21.99
3      65729284  Pakistan                   5.00
4      25819711  United States              7.50
5      62837458  United States              44.55
6      88379852  China                      99.28

我嘗試編寫的函數是這樣的:

updateCountry <- function(x) {
  if (clients$Country == "US") {
        clients$CountryType <- "US"
  } else {
    clients$CountryType <- "Foreign"
    }
}

然后,我將其應用為:

sapply(clients, updateCountry)

當我對數據sapply的頭部執行sapply ,得到以下信息:

"US" "US" "US" "US" "US" "US" 
Warning messages:
1: In if (clients$Country == "United States") { :
  the condition has length > 1 and only the first element will be used
2: In if (clients$Country == "United States") { :
  the condition has length > 1 and only the first element will be used
3: In if (clients$Country == "United States") { :
  the condition has length > 1 and only the first element will be used
4: In if (clients$Country == "United States") { :
  the condition has length > 1 and only the first element will be used
5: In if (clients$Country == "United States") { :
  the condition has length > 1 and only the first element will be used
6: In if (clients$Country == "United States") { :
  the condition has length > 1 and only the first element will be used

似乎該函數正確地對國家/地區進行了分類,但是沒有正確地更新clients $ CountryType列。 我究竟做錯了什么? 另外-這是完成更新數據框的最佳方法嗎?

ifelse似乎是您實際想要的。 它是if / else構造的向量化版本。

 clients$CountryType <- ifelse(clients$Country == "US", "US", "Foreign")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM