How can I translate the column CLASS, so that I get a new column CLASS2 with "1" = "positive", "-1" = "negative", "0" = "neutral". I know this is a very basic question, and I think one can use ifelse()
for this. But I just don't know how to use that function correctly.
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
The output should look like this:
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
Thank You!
Here's a simple way to do it using a helper function and 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))
Or you can rewrite translate
using ifelse
to make it more compact:
translate <- function(x) {
ifelse(x == '1', 'positive', ifelse(x == '-1', 'negative', 'neutral'))
}
Both of these will produce the output you asked for. But there might be a better way.
...like the one @joran suggested, if CLASS
is of factor type (which probably it is):
df$CLASS2 <- c('negative','neutral','positive')[df$CLASS]
As @beginneR pointed out, you don't need a function in my first two proposals. But I like to use functions for better readability.
这是一种一般的方法,使用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
is an option:
df %>%
mutate(
CLASS2 = case_when(
CLASS == 1 ~ 'positive',
CLASS == 0 ~ 'neutral',
CLASS == -1 ~ 'negative',
TRUE ~ '?'
)
)
Super readable, isn't it?
Although if you had more levels in CLASS
, typing all those CLASS ==
conditions would've been cumbersome. In that case, IMHO, sapply
is indeed the best option. Or purrr::map
for that matter!
> 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"
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.