Question: I want to convert a certain list of strings into separated lists inside another list, which will contain strings and floats.
I've tried to use the `append` method to get the result, but I'm having trouble on making the nested list. Is there a way to get only the last line of my `output` as the result? This is my code:

```
def func(L):
    n = []
    lists = [i.split(',') for i in L]
    for xlist in lists:
        xlist[1:] = [float(item) for item in xlist[1:]]
        n.append(xlist)
        print(n)

if __name__ == '__main__':
    func(['PersonX, 10, 92, 70', 'PersonY, 60, 70', 'PersonZ, 98.5, 1100, 95.5, 38'])

OUTPUT: [['PersonX', 10.0, 92.0, 70.0]]
        [['PersonX', 10.0, 92.0, 70.0], ['PersonY', 60.0, 70.0]]
        [['PersonX', 10.0, 92.0, 70.0], ['PersonY', 60.0, 70.0], ['PersonZ', 98.5, 1100.0, 95.5, 38.0]]

EXPECTED OUTPUT: [['PersonX', 10.0, 92.0, 70.0], ['PersonY', 60.0, 70.0], ['PersonZ', 98.5, 1100.0, 95.5, 38.0]]

```

Thanks in advance for any help.

Answer: 
You can combine your two approaches:

```
    df %>%
      split(~test_nr) %>%
      map_dfr(~ .x %>% 
                add_row(test_id = .$test_id[1], 
                        test_nr = .$test_nr[1], 
                        region = "mean",
                        test_value = mean(.$test_value)))  

```
You could achieve your target with this Base R one-liner:

```
merge( df, aggregate( df, by = list( df$test_nr ), FUN = mean ), all = TRUE )[ , 1:4 ]

```

`aggregate` provides you with the lines you need, and `merge` inserts them into the right places of your dataframe. You don't need the last column of the combined dataframe, so use only the first four columns. The code produces some warnings for the `region` column which can be disregarded. In the region column, the function (`MEAN`) is not displayed.

Making it a little more generic:

```
f <- "mean"
df1 <- merge( df, aggregate( df, by = list( df$test_id, df$test_nr ),
                             FUN = f ), all = TRUE )[ , 1:4 ]
df1$region[ is.na( df1$region ) ] <- toupper( f )

```

Here, you aggregate also by `test_id`, you can change the function you are using in one place, and you have it printed in the `region` column:

```
> df1
   test_id test_nr region test_value
1        1       1      A       3.00
2        1       1      B       1.00
3        1       1      C       1.00
4        1       1      D       2.00
5        1       1   MEAN       1.75
6        1       2      A       4.00
7        1       2      B       2.00
8        1       2      C       4.00
9        1       2      D       1.00
10       1       2   MEAN       2.75

```