遍历R中列名的特定子集 [英] Looping over a specific subset of column names in R

查看:134
本文介绍了遍历R中列名的特定子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框AData,我已经提取了它的列名称的一个子集,说SpecialName。我想知道如何在for循环中引用这些列。



我现在的代码如下所示:

< pre $ SpecialNames< - setdiff(colnames(AData),colnames(BData))

for(i in SpecialNames){

AData $ i< - NULL#对AData $ i做某些事情,例如删除它或其他东西

}

唉,AData $我似乎并没有引用名称为i的数据框AData的列。有没有一种不同的语法可以给我这个?



我在这篇文章中读到指出:$是用于交互式的使用,而是在编程时,也就是当列名需要使用[或[[,因此我用示例[[paste0(i,'.impt')]]替换了示例$ i.imp。
$ b $基于这个评论,我猜想,我一直在寻找的语法可能是AData $ [i]或AData $ [[i]]或AData $ [[paste0(i)]],但是没有一个看起来像这样任何想法?

不知道是什么你在做什么,很难说是否是循环的路径。然而,希望这将有助于你的方式:

  ##示例数据总是很好
set.seed (1)
mydf < - data.frame(A = c(1,1,1,2,2,3,3,3,4,4,4),
B =字母[c(1,1,1,1,2,2,2,2,2,2,2)],
矩阵(样本(100,36,替换= TRUE),nrow = 12 ))

##这是您的特殊向量名称
特殊名称< - setdiff(名称(mydf),c(A,B))

##这是一个`for`循环,它将打印在specialnames中命名的每列的前两行
##
##这不是我通常会在R中执行的
## -------------------------------------------- -----------
for(i in seq_along(specialnames)){
print(head(mydf [specialnames [i]],2))
}






注意事项(也许):


$ b $ ul
  • for(i in seq_along(specialnames)) seq_along (或 i in 1:length(sp eicamenames)或类似的东西)是很重要的。

  • 你似乎误解了 [ [[。尝试以下方法来了解它们的作用:


    I have a dataframe AData that of which I have extracted a certain subset of its column names say SpecialNames. I would like to know how to reference these columns in a for loop.

    My current code looks like this:

    SpecialNames <- setdiff(colnames(AData), colnames(BData))
    
    for ( i in SpecialNames ) {
    
        AData$i <- NULL # Do something to AData$i such as delete it or something else
    
    }
    

    Alas, AData$i does not seem to reference the column of dataframe AData with name i. Is there a different syntax that would give me that?

    I read in this post here that: "the $ is for interactive usage. Instead, when programming, i.e. when the column name is to be interpreted, you need to use [ or [[, hence I replaced sample$i.imp with sample[[paste0(i, '.impt')]]".

    Based on this comment, I guessed that perhaps the syntax I have been looking for is AData$[i] or AData$[[i]] or AData$[[paste0(i)]] but none of these seem to work either.

    Any ideas?

    解决方案

    Not knowing what you are doing, it's hard to say whether a for loop is the way to go or not; however, hopefully this will help get you on your way:

    ## Sample data is always nice
    set.seed(1)
    mydf <- data.frame(A = c(1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4, 4),
                       B = LETTERS[c(1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2)],
                       matrix(sample(100, 36, replace = TRUE), nrow = 12))
    
    ## Here is your vector of special names
    specialnames <- setdiff(names(mydf), c("A", "B"))
    
    ## Here is a `for` loop that will print the first two rows
    ##   of each column named in "specialnames"
    ## THIS IS NOT HOW I WOULD NORMALLY DO THIS IN R
    ## -------------------------------------------------------
    for (i in seq_along(specialnames)) {
      print(head(mydf[specialnames[i]], 2))
    }
    


    Matters of note (perhaps):

    • for (i in seq_along(specialnames)): That seq_along (or i in 1:length(specialnames) or something like that) is important.
    • You seem to have misunderstood the use of [ and [[. Try the following to get a sense of what they do:
      • mydf["A"]
      • mydf[["A"]]
      • mydf[1, c("A", "B")]
    • Two questions to look at here and here.

    这篇关于遍历R中列名的特定子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆