Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

add a categorical variable to data frame based on fixed rows order in r

I have a data frame like this:

Q1 <- c("A",NA,"A",NA,"NA","C","D","A","B", NA) 
Q2 <- c("D",NA,"D","C",NA,NA,"A","A","A","A")
Q3 <- c("B","B","C","A",NA,"A","B","D","E",NA)
Q4 <- c("B",NA,"C","C","C","C","D","B",NA,"A")
Q5 <- c("A",NA,"D",NA,NA,"C",NA,"B","B", NA)
Q6 <- c("D",NA,"D","C",NA,NA,"A","A","A","A")
mydf <- data.frame(Q1,Q2,Q3,Q4,Q5,Q6)
mydf

What I want to do is add a new column "subject" to my dataset and assign every 2 rows a category of a subject as below. I know adding a new column is done with mutate but I couldn’t find how to tell R to do that on a fixed rows interval.

    Q1   Q2   Q3   Q4   Q5   Q6    Subject
1     A    D    B    B    A    D   Language
2  <NA> <NA>    B <NA> <NA> <NA>   Language
3     A    D    C    C    D    D   Science
4  <NA>    C    A    C <NA>    C   Science
5    NA <NA> <NA>    C <NA> <NA>   Math 
6     C <NA>    A    C    C <NA>   Math
7     D    A    B    D <NA>    A   Art
8     A    A    D    B    B    A   Art
9     B    A    E <NA>    B    A   History
10 <NA>    A <NA>    A <NA>    A   History

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

How about this:

library(dplyr)

Q1 <- c("A",NA,"A",NA,"NA","C","D","A","B", NA) 
Q2 <- c("D",NA,"D","C",NA,NA,"A","A","A","A")
Q3 <- c("B","B","C","A",NA,"A","B","D","E",NA)
Q4 <- c("B",NA,"C","C","C","C","D","B",NA,"A")
Q5 <- c("A",NA,"D",NA,NA,"C",NA,"B","B", NA)
Q6 <- c("D",NA,"D","C",NA,NA,"A","A","A","A")
mydf <- data.frame(Q1,Q2,Q3,Q4,Q5,Q6)
mydf
#>      Q1   Q2   Q3   Q4   Q5   Q6
#> 1     A    D    B    B    A    D
#> 2  <NA> <NA>    B <NA> <NA> <NA>
#> 3     A    D    C    C    D    D
#> 4  <NA>    C    A    C <NA>    C
#> 5    NA <NA> <NA>    C <NA> <NA>
#> 6     C <NA>    A    C    C <NA>
#> 7     D    A    B    D <NA>    A
#> 8     A    A    D    B    B    A
#> 9     B    A    E <NA>    B    A
#> 10 <NA>    A <NA>    A <NA>    A

mydf %>% 
  mutate(Subject = rep(c("Language", "Science", 
                         "Math", "art", "History"), 
                       each=2))
#>      Q1   Q2   Q3   Q4   Q5   Q6  Subject
#> 1     A    D    B    B    A    D Language
#> 2  <NA> <NA>    B <NA> <NA> <NA> Language
#> 3     A    D    C    C    D    D  Science
#> 4  <NA>    C    A    C <NA>    C  Science
#> 5    NA <NA> <NA>    C <NA> <NA>     Math
#> 6     C <NA>    A    C    C <NA>     Math
#> 7     D    A    B    D <NA>    A      art
#> 8     A    A    D    B    B    A      art
#> 9     B    A    E <NA>    B    A  History
#> 10 <NA>    A <NA>    A <NA>    A  History

Created on 2022-11-17 by the reprex package (v2.0.1)

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading