0
$\begingroup$

I have a dataframe in R containing a variable for programming languages. This is extracted from a multiple choice questionnaire in a survey. As a result, any programming language may fall into the beginning, middle or end of the whole string. I added a binary variable for any programming language. Here is a sample of my data:

language <- c(
"Ruby, Assembly, R, Go, Rust",
"Java, Javascript, Ruby, SQL",
"Java, Ruby",
"C, C++",
"PHP, Javascript, Ruby, Assembly, Swift, R, Matlab, Go, Haskell",
"R",
"Perl, Javascript, R",
"Javascript, Ruby, Bash",
"Python, PHP, Javascript",
"Java",
"Java, C"
 )

df <-as.data.frame(language,stringsAsFactors = FALSE)
df$python      <- 0
df$java        <- 0
df$c           <- 0
df$r           <- 0
df$d           <- 0

for(trow in (1:nrow(data)))
{if (length (grep(pattern="Python",x=df[trow,"language"]))  > 0)    {df[trow,"python"] <- 1}}

I have problem with one-word languages such as C, R, D and Java . I made the variables for C#, C++ , Javascript, Ruby and others using the for loop but I have problems with these four. What regular expression can I use so that it covers Java but not Javascript (in the beginning, middle and end of the string) and covers R but not Ruby and so on? Any other solution for making the binary variables are also welcome.

$\endgroup$
5
  • $\begingroup$ I'm voting to close this question as off-topic because again this is a programming question, contains nothing that could be called data science, and there is an entire stack exchange site, stackoverflow.com, full of people wanting to help with programming questions. $\endgroup$
    – Spacedman
    Jun 29, 2016 at 6:36
  • $\begingroup$ I think R questions are generally all on-topic for this SE, even if they are also viable for SO and often better there. I'm reluctant to close such a question here. $\endgroup$
    – Sean Owen
    Jun 29, 2016 at 13:16
  • $\begingroup$ @HamidehIraj What should the result look like? $\endgroup$
    – lukeA
    Jun 29, 2016 at 19:04
  • $\begingroup$ The results are the binary variables such as java, r,... filled correctly with zero and one. $\endgroup$
    – Hamideh
    Jun 29, 2016 at 19:25
  • $\begingroup$ Feels like an x-y-problem - I don't think you need regular expressions and loops to get the final result. $\endgroup$
    – lukeA
    Jun 30, 2016 at 0:41

2 Answers 2

1
$\begingroup$

You could do

reshape2::recast(
  data = setNames(strsplit(language, ", ", T), language), 
  formula = L1~value, 
  fun.aggregate = length
)
#                                                                L1 Assembly Go R Ruby Rust Java Javascript SQL C C++ Haskell Matlab PHP Swift Perl Bash Python
# 1                                                          C, C++        0  0 0    0    0    0          0   0 1   1       0      0   0     0    0    0      0
# 2                                                            Java        0  0 0    0    0    1          0   0 0   0       0      0   0     0    0    0      0
# 3                                                         Java, C        0  0 0    0    0    1          0   0 1   0       0      0   0     0    0    0      0
# 4                                     Java, Javascript, Ruby, SQL        0  0 0    1    0    1          1   1 0   0       0      0   0     0    0    0      0
# 5                                                      Java, Ruby        0  0 0    1    0    1          0   0 0   0       0      0   0     0    0    0      0
# 6                                          Javascript, Ruby, Bash        0  0 0    1    0    0          1   0 0   0       0      0   0     0    0    1      0
# 7                                             Perl, Javascript, R        0  0 1    0    0    0          1   0 0   0       0      0   0     0    1    0      0
# 8  PHP, Javascript, Ruby, Assembly, Swift, R, Matlab, Go, Haskell        1  1 1    1    0    0          1   0 0   0       1      1   1     1    0    0      0
# 9                                         Python, PHP, Javascript        0  0 0    0    0    0          1   0 0   0       0      0   1     0    0    0      1
# 10                                                              R        0  0 1    0    0    0          0   0 0   0       0      0   0     0    0    0      0
# 11                                    Ruby, Assembly, R, Go, Rust        1  1 1    1    1    0          0   0 0   0       0      0   0     0    0    0      0
$\endgroup$
5
  • $\begingroup$ How can I reorder the new dataframe according to the order in the initial list? $\endgroup$
    – Hamideh
    Nov 7, 2018 at 4:35
  • $\begingroup$ or any other way of keeping the order in recast function? $\endgroup$
    – Hamideh
    Nov 7, 2018 at 5:09
  • $\begingroup$ @Hamideh df2 <- reshape2::recast( data = setNames(strsplit(language, ", ", T), language), formula = L1~value, fun.aggregate = length ); df2[match(language, df2$L1), ]? $\endgroup$
    – lukeA
    Nov 7, 2018 at 12:22
  • $\begingroup$ yes @lukeA it does not retain the order $\endgroup$
    – Hamideh
    Nov 9, 2018 at 2:34
  • $\begingroup$ @Hamideh Tried match as shown in the comment above? $\endgroup$
    – lukeA
    Nov 9, 2018 at 11:13
0
$\begingroup$

Your examples are well-formatted; this answer assumes that's the reality of your data, and not just the examples you've provided.

It seems like you could split on spaces and strip commas, then just pick out languages based on exact equality (this would not have a problem for Java vs. JavaScript, or R vs. Ruby).

If you prefer to use regular expressions, it seems like you could search for language+comma or language+(end of string). E.g. for searching for familiarity with R, you could do something like this:

R[ ,$]

So it will match R if and only if R is followed by a space or comma (indicating the end of the language) or the end of the string (indicated by a dollar sign).

If you wanted to be doubly safe (though this won't matter in your examples) you could even do:

[, ^]R[ ,$]

Which will only match R if both the beginning and the end are among the boundary of the string, a space, or a comma.

Replace R with whatever the full name of your programming language is.

This should all work so long as both of the following are satisfied:

  1. Your languages are all spaced out by either commas or spaces or both.
  2. None of your languages include commas or spaces (and even then it should be fine unless they interact in funny ways, e.g. a language "Go" and another language "Go Home").

Hope this helps.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.