【问题标题】:Loop through a list of data frames and return names of columns not containing duplicate entries? [closed]循环遍历数据框列表并返回不包含重复条目的列名称? [关闭]
【发布时间】:2014-06-09 06:51:26
【问题描述】:

遍历数据框列表并返回仅包含唯一值、不包含重复条目的列的名称。

【问题讨论】:

    标签: r dataframe unique lapply sapply


    【解决方案1】:
    names(dfrm)[ sapply(dfrm, function(x) length(x) == length(unique(x)) ) ]
    

    【讨论】:

    • 感谢 BondedDust。 vrajs5 添加了我启动和运行所需的最后一部分。
    【解决方案2】:

    我会给 BondedDust 的答案 +1,因为我也将要写几乎相同的答案......另外,由于约翰想为给定的 data.frames 列表列出这样的列名,我在行中添加了以下内容

    #dfList is list of dataframes for which operation is needed
    myfun = function(df){
              colnames(df)[ sapply(df, function(x) length(x) == length(unique(x)) ) ]}
    
    lapply(dfList, FUN = myfun)
    

    希望对你有帮助……

    【讨论】:

    • 谢谢,完美运行。
    猜你喜欢
    • 1970-01-01
    • 2019-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-28
    • 2020-08-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多