extracting and transferring characters

Hello,
I cannot seem to figure out how to extract characters from strings and then use them to fill in rows in a different dataframe. I have a dataframe like

subject     string
1               dkid
1              sgua
1              sga
2               cpr
2              bhja
...

and another dataframe that looks like

subject.    character.    yes/no
1                  a                     NA
1                  b                    NA
1                  c                    NA
1                  d                    NA
...

I am trying to extract the individual characters from every row in the 'string' column of the first dataframe for each subject and figure out whether or not every possible character (from the 'character' column in the second dataframe) is there, i.e., I want to populate the 'yes/no' column based on if the strings from the first dataframe contain the characters from the second dataframe, for each subject.

Any help would be immensely appreciated!

What you did so far? How your code looks like? What have you tried? substring() or strsplit()?

I have tried things along these lines..

for(j in unique(items_wCode$subject...3)){
  for(i in 1:length(items_wCode$items)){

    items_list[i] = strsplit(items_wCode$items[i],"")
  }
  append(j_list,items_list)
}
}

for(i in unique(items_df$subject)){
  for(j in unique(chars)){
    if(chars[j] %in% unlist(items_list$i)){
      items_df[which(items_df$item == chars[j]),4] = 1
    }else{
      items_df[which(items_df$item == chars[j]),4] = 0
    }
  }

if(isTRUE(items_df$item[1] == chars[1])){
  items_df[which(items_df$item == chars[1]),4] = 1
}

I can split the strings but can't populate the new dataframe with the extracted characters.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.