How to extract a data.frame from string data

December 28, 2014

A guest article by Asher Raz, PhD, CareerHarmony

Sometimes, data of subjects are recorded on a server (e.g. SQL server) as string data records for each subject. In some cases we need only a part of those string data for each subject and we need it as numerical data (e.g. as a data.frame). How can we get the required data?

In the following post I would like to share my experience with this issue.

Below is a sample of string data:


Each string data is organized according to rules. It could be that the required data is organized according to one rule at the beginning of the sting data and according to another rule in the rest of the string data.

Firstly, we need to find the rules that organize the required data. That is done by checking the data thoroughly. For instance, in the sample string data mentioned above, the required data are numbers that appears after four 0’s. The first datum we need is 4, the second is 1, and so on. The first datum appears in the 45th place in the string data, the second in 46th place after the first datum and so on. In order to record those rules, we can create a vector that contains the places of all the required data in the string:

#Creating the counter vector.
counter <- c(45) 
i <- 45 
for(n in 1:6) { 
  if (n<6) { 
    i <- i+46 
  } else { 
    i <- i+91 
  countert <- c(i) 
  counter <- rbind(counter, countert) 
counter <- as.vector (counter) 
for(n in 1:30) { 
  for(n in 1:6) { 
    if (n<6) { 
      i <- i+46 
    } else { 
      i <- i+91 
    countert <- c(i) 
    counter <- c(counter, countert) 

Secondly, we need to input into R the string data (in this example from SQL server) and then prepare the data.frame. That can be done by the following code:

#Read the answers from the text files.
library (RODBC)
myconn <-odbcConnect("Subjects_Data", uid="ab", pwd="cde")
NEW-DATA_sql_data_RawAnswer <- sqlQuery(myconn, "select top 1000 RawAnswer from New-Data",stringsAsFactors = FALSE)
NEW-DATA_all_sample <-c()
list_item <- 14
for(RawAnswer in NEW-DATA_sql_data_RawAnswer$RawAnswer) {
save(NEW-DATA_sql_data_RawAnswer, file=" C:/R_DATA/NEW-DATAt.txt ",ascii=T)
NEW-DATA_string <- readLines("C:/R_DATA/NEW-DATAt.txt")
list_item <- list_item + 3
NEW-DATA_stringt <- NEW-DATA_string[c(list_item)]
NEW-DATA_one_subject <- c()
for(n in counter) {
  NEW-DATAt <- substring(NEW-DATA_stringt, n,n)
  NEW-DATA_one_subject <- rbind(NEW-DATA_one_subject, NEW-DATAt)
NEW-DATA_one_subject <- as.numeric(NEW-DATA_one_subject)
NEW-DATA_one_subject <- NEW-DATA_one_subject [c(-187)]
NEW-DATA_all_sample <- rbind(NEW-DATA_all_sample, NEW-DATA_one_subject)
NEW-DATA_all_sample_df<, 2, as.numeric))
NEW-DATA_sql_data_CandidateID_Username <- sqlQuery(myconn, "select top 1000 CandidateID,Username from New-Data",stringsAsFactors = FALSE)
NEW-DATA_all_sample_df$CandidateID <- NEW-DATA_sql_data_CandidateID_Username_df$CandidateID
NEW-DATA_all_sample_df$Username <- NEW-DATA_sql_data_CandidateID_Username_df$Username
NEW-DATA_all_sample_df <- NEW-DATA_all_sample_df[,c(187,188,1:186)]
write.csv(NEW-DATA_all_sample_df,file = "C:/R_DATA/NEW-DATA_all_sample.csv")


It should be noted that the string data that was imported to R had to be saved into a text file, before it can be read by R (see above the commands before the command NEW-DATA_string <- readLines(“C:/R_DATA/NEW-DATAt.txt”)). This command can’t work directly on the data that was imported from SQL server.

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)