How to convert this Python code to R?
<p>,I need help with translating a python code into an R code.</p>

<p>I have a data frame df, that contains the column IndicatorOfDefault and I would like too generate a column named indvalues.</p>

<p>Example:</p>

<pre><code>row number   IndicatorOfDefault   indvalues

823602                        P           0 

823603                        P           0

823604                  N1,N13,           8

823605                      N1,           1

823606                        P           0

823607         N1,N2,N3,N9,N10,          13

823608                        P           0
</code></pre>

<p>The code that I want to convert is the following:</p>

<pre><code>df['indicators'] = df['IndicatorOfDefault'].str.split(',')

Nvalues = {'' : -1, 'P' : 0, 'N1' : 1, 'N2' : 2, 'N11' : 3, 'N12' : 4, 'N3' : 5, 'N4' : 6, 
           'N6' : 7, 'N10' : 8, 'N13' : 9, 'N5' : 10, 'N7' : 11, 'N8': 12, 'N9' : 13}

df['indvalues'] = df['indicators'].apply(lambda x: max([Nvalues.get(y,y) for y in x ]))
</code></pre>

<p>I would like to execute the same code in R, but I don't know how to write it in R. </p>

<p>Can anyone help me please?</p>

<p>Thanks in advance</p>

<p>Why is this question off topic? I don't understand what's wrong... I am new to this site, so I would appreciate if someone could explain why this specific problem doesn't belong here? I've read what's written in the help center, but i still don't know what's wrong.</p>

<p>I've managed to solve my problem in a different way. I get the result that I want - the most important indicator (it didn't needed to be a number necessary).</p>

<pre><code>    df$ind &lt;- "P"
for(i in c(1, 2, 11, 12, 3, 4, 6, 10, 13, 5, 7, 8, 9)){
       df &lt;- transform(df, ind = ifelse(grepl(as.character(paste0("N",i,",")),IndicatorOfDefault),as.character(paste0("N",i)),ind))
    }
</code></pre>

<p>Example:</p>

<pre><code>row number   IndicatorOfDefault         ind

823602                        P           P 

823603                        P           P

823604                  N1,N13,         N13

823605                      N1,          N1

823606                        P           P

823607         N1,N2,N3,N9,N10,          N9

823608                        P           P
</code></pre>

<p>Since R does not have the dictionary object, the best translation will be a named list. Do note: R does not allow zero-length names, so <em>N0</em> is used. From there you can use R's <code>vapply()</code> and <code>strsplit()</code> to string split the column and find its corresponding max value. Specifically, <code>vapply()</code> as opposed to the standard <code>lapply()</code> is used to specify the output type as a numeric vector for dataframe column. </p>

<p>Below includes both scripts to show exact <em>result</em> columns renders with posted data. </p>

<p><strong>Python</strong></p>

<pre class="lang-py prettyprint-override"><code>from io import StringIO
import pandas as pd

data = '''row number;IndicatorOfDefault;indvalues
823602;P;0
823603;P;0
823604;N1,N13,;8
823605;N1,;1
823606;P;0
823607;N1,N2,N3,N9,N10;13
823608;P;0'''

df = pd.read_table(StringIO(data), sep=";")

df['indicators'] = df['IndicatorOfDefault'].str.split(',')

Nvalues = {'' : -1, 'P' : 0, 'N1' : 1, 'N2' : 2, 'N11' : 3, 'N12' : 4, 'N3' : 5, 'N4' : 6, 
           'N6' : 7, 'N10' : 8, 'N13' : 9, 'N5' : 10, 'N7' : 11, 'N8': 12, 'N9' : 13}

df['result'] = df['indicators'].apply(lambda x: max([Nvalues.get(y,y) for y in x ]))

print(df)
#    row number IndicatorOfDefault  indvalues             indicators  result
# 0      823602                  P          0                    [P]       0
# 1      823603                  P          0                    [P]       0
# 2      823604            N1,N13,          8            [N1, N13, ]       9
# 3      823605                N1,          1                 [N1, ]       1
# 4      823606                  P          0                    [P]       0
# 5      823607    N1,N2,N3,N9,N10         13  [N1, N2, N3, N9, N10]      13
# 6      823608                  P          0                    [P]       0
</code></pre>

<p><strong>R</strong></p>

<pre class="lang-r prettyprint-override"><code>data = 'row number;IndicatorOfDefault;indvalues
823602;P;0
823603;P;0
823604;N1,N13,;8
823605;N1,;1
823606;P;0
823607;N1,N2,N3,N9,N10;13
823608;P;0'

df = read.table(text=data, sep=";", header=TRUE, stringsAsFactors = FALSE)

Nvalues = list(N0=-1, P=0, N1=1, N2=2, N11=3, N12=4,
               N3=5, N4=6, N6=7, N10=8, N13=9, N5=10,
               N7=11, N8=12, N9=13)

df$indicators &lt;- lapply(df$IndicatorOfDefault, function(i) {
     as.character(strsplit(i, ",")[[1]])
})

df$result &lt;- vapply(df$indicators, function(i) {  
     max(as.numeric(Nvalues[i]))  
}, numeric(1))

df

#   row.number IndicatorOfDefault indvalues          indicators result
# 1     823602                  P         0                   P      0
# 2     823603                  P         0                   P      0
# 3     823604            N1,N13,         8             N1, N13      9
# 4     823605                N1,         1                  N1      1
# 5     823606                  P         0                   P      0
# 6     823607    N1,N2,N3,N9,N10        13 N1, N2, N3, N9, N10     13
# 7     823608                  P         0                   P      0
</code></pre>

python dataframe