How to access single elements in a table in R

后端 未结 3 639
梦谈多话
梦谈多话 2020-12-23 20:52

How do I grab elements from a table in R.

My Data looks like this:

         V1     V2
1      12.448 13.919
2      22.242  4.606
3           


        
3条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-23 21:25

    ?"[" pretty much covers the various ways of accessing elements of things.

    Under usage it lists these:

    x[i]
    x[i, j, ... , drop = TRUE]
    x[[i, exact = TRUE]]
    x[[i, j, ..., exact = TRUE]]
    x$name
    getElement(object, name)
    
    x[i] <- value
    x[i, j, ...] <- value
    x[[i]] <- value
    x$i <- value
    

    The second item is sufficient for your purpose

    Under Arguments it points out that with [ the arguments i and j can be numeric, character or logical

    So these work:

    data[1,1]
    data[1,"V1"]
    

    As does this:

    data$V1[1]
    

    and keeping in mind a data frame is a list of vectors:

    data[[1]][1]
    data[["V1"]][1]
    

    will also both work.

    So that's a few things to be going on with. I suggest you type in the examples at the bottom of the help page one line at a time (yes, actually type the whole thing in one line at a time and see what they all do, you'll pick up stuff very quickly and the typing rather than copypasting is an important part of helping to commit it to memory.)

提交回复
热议问题