如何检查多个R对象的相同内容?

时间:2015-09-25 13:00:06

标签: r

假设我有一个列表对象,例如:

set.seed(123)
df <- data.frame(x = rnorm(5), y = rbinom(5,2,0.5))
rownames(df) <- LETTERS[1:5]
ls <- list(df1 = df, df2 = df, df3 = df)

我的问题是如何快速检查ls中三个元素(数据框)的行名是否相同。

2 个答案:

答案 0 :(得分:3)

你可以尝试

public class Word {
    public static void main(String args[]) {
        String rev = "This is a string";
        char[] a = new char[rev.length()];

        int i = 0;
        for (; i < a.length; i++) {
            a[i] = rev.charAt(i);
            if (rev.charAt(i) == ' ' && i + 1 < a.length) {
                if (rev.charAt(i+1) >= 'a' && rev.charAt(i+1) <= 'z') {
                    a[i + 1] = (char)(rev.charAt(i + 1) - 32);
                    i++;
                }
            }
        }  
        String title = new String(a);
        System.out.print(title);
    }
}

要仅检查第i列的名称,可以将其修改为

all(sapply(ls, rownames) == rownames(ls[[1]]))

答案 1 :(得分:1)

您可以使用以下命令获取行名列表:

Map(rownames, ls)

因此您可以检查所有数据帧是否具有相同的rownames,检查row.names vector只有一个唯一值:

length(unique(Map(rownames, ls))) == 1
相关问题