Frage

I have a list of dataframes representing clustered data.

    [[1]]
         x   y   r.value   g.value     b.value
    5   99  56 0.9176471 0.4941176 0.007843137
    8  192 136 0.8352941 0.6313725 0.000000000
    10 261  64 0.8588235 0.7254902 0.000000000
    12 355 379 0.5098039 0.2588235 0.078431373

    [[2]]
         x   y   r.value   g.value    b.value
    2   44 325 0.9960784 0.7254902 0.11764706
    3   52  65 0.9960784 0.7333333 0.37647059
    9  211  25 0.8078431 0.6823529 0.59215686
    14 374 281 0.5882353 0.1882353 0.09019608

    [[3]]
         x   y   r.value   g.value    b.value
    1   25  68 0.9960784 0.7372549 0.36862745
    7  191 398 0.9529412 0.7411765 0.07058824
    11 338 125 0.9843137 0.8431373 0.02745098
    15 492 395 0.9764706 0.7411765 0.00000000

    [[4]]
         x   y   r.value   g.value   b.value
    4   99 250 0.9882353 0.8823529 0.4392157
    6  133 252 0.9960784 0.9450980 0.6705882
    13 362 372 0.8941176 0.8549020 0.6117647

I would like to use a wrapper function like lapply to append each dataframe's index within the list to the end of each dataframe. I would like the output to look like:

    [[1]]
         x   y   r.value   g.value     b.value cluster
    5   99  56 0.9176471 0.4941176 0.007843137       1
    8  192 136 0.8352941 0.6313725 0.000000000       1
    10 261  64 0.8588235 0.7254902 0.000000000       1
    12 355 379 0.5098039 0.2588235 0.078431373       1

    [[2]]
         x   y   r.value   g.value    b.value cluster
    2   44 325 0.9960784 0.7254902 0.11764706       2
    3   52  65 0.9960784 0.7333333 0.37647059       2
    9  211  25 0.8078431 0.6823529 0.59215686       2
    14 374 281 0.5882353 0.1882353 0.09019608       2

    [[3]]
         x   y   r.value   g.value    b.value cluster
    1   25  68 0.9960784 0.7372549 0.36862745       3
    7  191 398 0.9529412 0.7411765 0.07058824       3
    11 338 125 0.9843137 0.8431373 0.02745098       3
    15 492 395 0.9764706 0.7411765 0.00000000       3

    [[4]]
         x   y   r.value   g.value   b.value cluster
    4   99 250 0.9882353 0.8823529 0.4392157       4
    6  133 252 0.9960784 0.9450980 0.6705882       4
    13 362 372 0.8941176 0.8549020 0.6117647       4

I have tried lapply(clusters, function(x) cbind(x,cluster= 1)) , but that of course only appends the value 1 to every dataframe, instead of the values 1, 2, 3, 4. I've also tried lapply(clusters, function(x) {i=1:4; cbind(x,cluster= i)}) which I saw suggested on another stackoverflow page, but received the error:

Error in data.frame(..., check.names = FALSE) : 
  arguments imply differing number of rows: 3, 4

Any help would be much appreciated.

War es hilfreich?

Lösung

Perhaps you can try the following instead:

lapply(seq_along(clusters), function(x) cbind(clusters[[x]], cluster= x))

A minimal example:

set.seed(1)
mydf <- data.frame(V1 = rep(c("a", "b", "c"), c(2, 3, 1)),
                   V2 = rnorm(6))
clusters <- split(mydf, mydf$V1)
clusters
# $a
#   V1         V2
# 1  a -0.6264538
# 2  a  0.1836433
# 
# $b
#   V1         V2
# 3  b -0.8356286
# 4  b  1.5952808
# 5  b  0.3295078
# 
# $c
#   V1         V2
# 6  c -0.8204684

lapply(seq_along(clusters), function(x) cbind(clusters[[x]], cluster= x))
# [[1]]
#   V1         V2 cluster
# 1  a -0.6264538       1
# 2  a  0.1836433       1
# 
# [[2]]
#   V1         V2 cluster
# 3  b -0.8356286       2
# 4  b  1.5952808       2
# 5  b  0.3295078       2
# 
# [[3]]
#   V1         V2 cluster
# 6  c -0.8204684       3
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top