Extracting a numeric information align with ID from unstructured dataset in R

Viewed 24

I am trying to extract score information for each ID and for each itemID. Here how my sample dataset looks like.

df <- data.frame(Text_1 = c("Scoring", "1 = Incorrect","Text1","Text2","Text3","Text4", "Demo 1: Color Naming","Amarillo","Azul","Verde","Azul",
                            "Demo 1: Errors","Item 1: Color naming","Amarillo","Azul","Verde","Azul",
                            "Item 1: Time in seconds","Item 1: Errors",
                            "Item 2: Shape Naming","Cuadrado/Cuadro","Cuadrado/Cuadro","Círculo","Estrella","Círculo","Triángulo",
                            "Item 2: Time in seconds","Item 2: Errors"),
                  School.2 = c("Teacher:","DC Name:","Date (mm/dd/yyyy):","Child Grade:","Student Study ID:",NA, NA,NA,NA,NA,NA,
                             0,"1 = Incorrect responses",0,1,NA,NA,NA,0,"1 = Incorrect responses",0,NA,NA,1,1,0,NA,0),
                 X_Elementary_School..3 = c("Bill:","X District","10/7/21","K","123-2222-2:",NA, NA,NA,NA,NA,NA,
                               NA,"Child response",NA,NA,NA,NA,NA,NA,"Child response",NA,NA,NA,NA,NA,NA,NA,NA),
                 School.4 = c("Teacher:","DC Name:","Date (mm/dd/yyyy):","Child Grade:","Student Study ID:",NA, 0,NA,1,NA,NA,0,"1 = Incorrect responses",0,1,NA,NA,120,0,"1 = Incorrect responses",NA,1,0,1,NA,1,110,0),
                 Y_Elementary_School..2 = c("John:","X District","11/7/21","K","112-1111-3:",NA, NA,NA,NA,NA,NA,NA,"Child response",NA,NA,NA,NA,NA,NA,"Child response",NA,NA,NA,NA,NA,NA, NA,NA))

> df
                    Text_1                School.2 X_Elementary_School..3                School.4 Y_Elementary_School..2
1                  Scoring                Teacher:                  Bill:                Teacher:                  John:
2            1 = Incorrect                DC Name:             X District                DC Name:             X District
3                    Text1      Date (mm/dd/yyyy):                10/7/21      Date (mm/dd/yyyy):                11/7/21
4                    Text2            Child Grade:                      K            Child Grade:                      K
5                    Text3       Student Study ID:            123-2222-2:       Student Study ID:            112-1111-3:
6                    Text4                    <NA>                   <NA>                    <NA>                   <NA>
7     Demo 1: Color Naming                    <NA>                   <NA>                       0                   <NA>
8                 Amarillo                    <NA>                   <NA>                    <NA>                   <NA>
9                     Azul                    <NA>                   <NA>                       1                   <NA>
10                   Verde                    <NA>                   <NA>                    <NA>                   <NA>
11                    Azul                    <NA>                   <NA>                    <NA>                   <NA>
12          Demo 1: Errors                       0                   <NA>                       0                   <NA>
13    Item 1: Color naming 1 = Incorrect responses         Child response 1 = Incorrect responses         Child response
14                Amarillo                       0                   <NA>                       0                   <NA>
15                    Azul                       1                   <NA>                       1                   <NA>
16                   Verde                    <NA>                   <NA>                    <NA>                   <NA>
17                    Azul                    <NA>                   <NA>                    <NA>                   <NA>
18 Item 1: Time in seconds                    <NA>                   <NA>                     120                   <NA>
19          Item 1: Errors                       0                   <NA>                       0                   <NA>
20    Item 2: Shape Naming 1 = Incorrect responses         Child response 1 = Incorrect responses         Child response
21         Cuadrado/Cuadro                       0                   <NA>                    <NA>                   <NA>
22         Cuadrado/Cuadro                    <NA>                   <NA>                       1                   <NA>
23                 Círculo                    <NA>                   <NA>                       0                   <NA>
24                Estrella                       1                   <NA>                       1                   <NA>
25                 Círculo                       1                   <NA>                    <NA>                   <NA>
26               Triángulo                       0                   <NA>                       1                   <NA>
27 Item 2: Time in seconds                    <NA>                   <NA>                     110                   <NA>
28          Item 2: Errors                       0                   <NA>                       0                   <NA>

This sample dataset is limited only for two schools, two teachers and two students.

In this step, I need to extract student responses for each item. Wherever the first column has Item , I need to grab from there. I especially need to index the rows and columns columns rather than giving the exact row columns number since this will be for multiple datafiles and each files has different information. No need to grab the ..:Error part.

################################################################################
# @@ 2-extract the score information here
# #@ 1-grab item information from where "Item 1:.." starts

Here, rather than using row number, how to automate this part.

score<-df[c(7:11,13:17,20:26),c(seq(2,dim(df)[2],2))] # need to automate row and columns index here
score<-as.data.frame(t(score))
rownames(score)<-seq(1,nrow(score),1)
colnames(score)<-paste0('i',seq(1,ncol(score),1)) # assign col names for items
score<-apply(score,2,as.numeric) # only keep numeric columns
score<-as.data.frame(score)
score$total<-rowSums(score,na.rm=T); score # create a total score

> score
  i1 i2 i3 i4 i5 i6 i7 i8 i9 i10 i11 i12 i13 i14 i15 i16 i17 total
1 NA NA NA NA NA NA  0  1 NA  NA  NA   0  NA  NA   1   1   0     3
2  0 NA  1 NA NA NA  0  1 NA  NA  NA  NA   1   0   1  NA   1     5

Additionally, I need to add ID which I could not achieve here. My desired output would be:

> score
      ID          i1 i2 i3 i4 i5 i6 i7 i8 i9 i10 i11 i12 i13 i14 i15 i16 i17 total
    1 123-2222-2  NA NA NA NA NA NA  0  1 NA  NA  NA   0  NA  NA   1   1   0     3
    2 112-1111-3   0 NA  1 NA NA NA  0  1 NA  NA  NA  NA   1   0   1  NA   1     5
0 Answers
Related