簡體   English   中英

如何將向量的值“轉換”為R中的另一個向量

[英]how to “translate” values of a vector into another vector in R

如何轉換CLASS列,以便獲得新的CLASS2列,其值為“ 1” =“正”,“-1” =“負”,“ 0” =“中性”。 我知道這是一個非常基本的問題,我認為可以ifelse()使用ifelse() 但是我只是不知道如何正確使用該功能。

DATE <- c("01.01.2000","02.01.2000","03.01.2000","06.01.2000","07.01.2000","09.01.2000","10.01.2000","01.01.2000","02.01.2000","04.01.2000","06.01.2000","07.01.2000","09.01.2000","10.01.2000")
RET <- c(-2.0,1.1,3,1.4,-0.2, 0.6, 0.1, -0.21, -1.2, 0.9, 0.3, -0.1,0.3,-0.12)
CLASS <- c("1","-1","0","1","1","-1","0","1","-1","-1","1","0","0","0")
df <- data.frame(DATE, RET, CLASS)

df

輸出應如下所示:

DATE <- c("01.01.2000","02.01.2000","03.01.2000","06.01.2000","07.01.2000","09.01.2000","10.01.2000","01.01.2000","02.01.2000","04.01.2000","06.01.2000","07.01.2000","09.01.2000","10.01.2000")
RET <- c(-2.0,1.1,3,1.4,-0.2, 0.6, 0.1, -0.21, -1.2, 0.9, 0.3, -0.1,0.3,-0.12)
CLASS <- c("1","-1","0","1","1","-1","0","1","-1","-1","1","0","0","0")
CLASS2 <- c("positive", "negative", "neutral", "positive", "positive", "negative", "neutral", "positive", "negative", "negative", "positive", "neutral", "neutral", "neutral")
df <- data.frame(DATE, RET, CLASS, CLASS2)

df

#          DATE   RET CLASS   CLASS2
# 1  01.01.2000 -2.00     1 positive
# 2  02.01.2000  1.10    -1 negative
# 3  03.01.2000  3.00     0  neutral
# 4  06.01.2000  1.40     1 positive
# 5  07.01.2000 -0.20     1 positive
# 6  09.01.2000  0.60    -1 negative
# 7  10.01.2000  0.10     0  neutral
# 8  01.01.2000 -0.21     1 positive
# 9  02.01.2000 -1.20    -1 negative
# 10 04.01.2000  0.90    -1 negative
# 11 06.01.2000  0.30     1 positive
# 12 07.01.2000 -0.10     0  neutral
# 13 09.01.2000  0.30     0  neutral
# 14 10.01.2000 -0.12     0  neutral

謝謝!

這是使用輔助函數和sapply進行操作的一種簡單方法:

translate <- function(x) {
  if (x == '1') {
    'positive'
  } else if (x == '-1') {
    'negative'
  } else {
    'neutral'
  }
}
df <- data.frame(DATE, RET, CLASS, CLASS2=sapply(CLASS, translate))

或者,您可以使用ifelse重寫translate以使其更緊湊:

translate <- function(x) {
  ifelse(x == '1', 'positive', ifelse(x == '-1', 'negative', 'neutral'))
}

兩者都會產生您要求的輸出。 但是也許有更好的方法。

...就像@joran建議的那樣,如果CLASS是因子類型(可能是):

df$CLASS2 <- c('negative','neutral','positive')[df$CLASS]

正如@beginneR指出的那樣,在我的前兩個建議中不需要使用函數。 但是我喜歡使用函數來提高可讀性。

這是一種一般的方法,使用match可以將其應用於更多級別:

CLASS2 <- c('positive','negative','neutral')[ match(CLASS, c('1','-1','0') ) ]

您甚至不需要定義一個函數並使用sapply ,只需創建一個新列並直接在其上使用ifelse即可:

df$Class2 <- with(df, ifelse(CLASS == '1', 'positive', ifelse(CLASS == '-1', 'negative', 'neutral')))

dplyr::case_when是一個選項:

df %>%
  mutate(
    CLASS2 = case_when(
      CLASS ==  1 ~ 'positive',
      CLASS ==  0 ~ 'neutral',
      CLASS == -1 ~ 'negative',
      TRUE ~ '?'
    )
  )

超級可讀,不是嗎?

盡管如果您在CLASS擁有更多級別,則鍵入所有那些CLASS ==條件將很麻煩。 在這種情況下,恕我直言, sapply的確是最好的選擇。 或者說purrr::map

> x <- c(-1, -1, 0, 1, -1) %>% as.character()
> x %>% map(~ list(`-1` = 'negative', `0` = 'neutral', `1` = 'positive')[[.x]]) %>% unlist()
[1] "negative" "negative" "neutral"  "positive" "negative"

暫無
暫無

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

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