[英]if else loop to create a new categorical variable
i would like to get a new variable lastocc
with the values 1 and 2. the variable lastocc
should show for each word if its last occurrence was shown also in the same color or not.我想获得一个值为 1 和 2 的新变量lastocc
。变量lastocc
应该为每个单词显示,如果它的最后一次出现也以相同的颜色显示。
So, in case when the particular word was shown now and previous time in the same color, it should be coded as 1. if in its last occurrence it was shown in difference color, it should be coded 2.因此,如果特定单词现在和以前以相同的颜色显示,则应编码为 1。如果在最后一次出现时以不同的颜色显示,则应编码为 2。
for example:例如:
trial word color lastocc
1 warm red
2 klein blue
3 ganz yellow
4 warm red 1
5 klein red 2
6 ganz yellow 1
7 klein red 1
i tried this code and its not working:我尝试了这段代码,但它不起作用:
data_expblocks$lastocc <- if (data_expblocks$word == TRUE & data_expblocks$color == TRUE) {lastocc = 1}
else { lastocc =2 }
hier is dput() =更高级的是 dput() =
structure(list(Subject = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L), Word = c("XXXX", "XXXX", "warm", "klein", "klein", "warm",
"ganz", "warm", "leicht", "ganz"), Colour = c("YELLOW", "RED",
"RED", "RED", "RED", "RED", "RED", "YELLOW", "RED", "YELLOW")), row.names = 53:62, class = "data.frame")
would be nice if you can advise me something.如果你能给我一些建议就好了。 thank you!谢谢你!
Here is one approach using dplyr
package.这是使用dplyr
package 的一种方法。
First, use group_by
to look at each word for a given subject.首先,使用group_by
查看给定主题的每个单词。 The only difference between rows should be the Colour
.行之间的唯一区别应该是Colour
。
Then, you can use mutate
to create a new column.然后,您可以使用mutate
创建一个新列。 Using ifelse
is will evaluate if the Colour
for a given row is the same as the previous Colour
.使用ifelse
is 将评估给定行的Colour
是否与前一个Colour
相同。 If it is, then the value will be 1. If not, it will be 2. If there is no prior value available (first time word appears), it will be NA
.如果是,则值为 1。如果不是,则为 2。如果没有可用的先前值(第一次出现单词),则为NA
。
library(dplyr)
data_expblocks %>%
group_by(Subject, Word) %>%
mutate(lastocc = ifelse(Colour == lag(Colour), 1, 2))
Output Output
Subject Word Colour lastocc
<int> <chr> <chr> <dbl>
1 1 XXXX YELLOW NA
2 1 XXXX RED 2
3 1 warm RED NA
4 1 klein RED NA
5 1 klein RED 1
6 1 warm RED 1
7 1 ganz RED NA
8 1 warm YELLOW 2
9 1 leicht RED NA
10 1 ganz YELLOW 2
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.