当我在R中输入列的元素作为输入时,如何获得整行?

时间:2015-12-28 10:56:27

标签: r dataframe

这是我的数据集:

 ui 194635691 194153563 177382028 177382031 195129144 196972549 196258704 194907960 196950156 194139014 153444738 192982501 192891196
1 237      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.01     0.000         0         0         0         0
2 261      0.01      0.00      0.00      0.00      0.00      0.00      0.00      0.00     0.000         0         0         0         0
3 290      0.00      0.00      0.01      0.01      0.00      0.00      0.00      0.00     0.000         0         0         0         0
4 483      0.00      0.00      0.00      0.00      0.00      0.01      0.00      0.00     0.000         0         0         0         0
5 485      0.00      0.00      0.00      0.00      0.00      0.00      0.00      0.00     0.027         0         0         0         0
6 533      0.00      0.01      0.00      0.00      0.00      0.00      0.00      0.00     0.000         0         0         0         0
7 534      0.00      0.00      0.00      0.00      0.08      0.00      0.00      0.00     0.000         0         0         0         0
8 535      0.00      0.01      0.00      0.00      0.00      0.00      0.00      0.00     0.000         0         0         0         0
9 536      0.00      0.00      0.00      0.00      0.00      0.00      0.01      0.00     0.000         0         0         0         0

user_id = actions_slippers $ ui [i]此用户ID来自其他数据框

我想获取user_id的行,我尝试了以下代码:

 for (i in 1:nrow(actions_slippers)) {

if (actions_slippers$w[i]==0.027) {
  user_id=actions_slippers$ui[i]

  for (j in 1:i) {
    mydf <- data.frame(
      ui = c(actions_slippers$ui[1:i]),
      w = c(actions_slippers$w[1:i]),
      iios = factor(
        c(actions_slippers$iios[1:i]),
        levels = unique(x)))

  a=  dcast(mydf, formula = ui ~ iios, 
          fill = 0, value.var = "w", 
          fun.aggregate = sum, drop = FALSE)
 a[a$ui %in% user_id, ]

我的目标是从数据帧中获取user_id的向量,然后计算user_id向量与数据帧中其他向量之间的余弦相似度

但它又回来了:

<0 rows> (or 0-length row.names)

谁能告诉我怎么做?

2 个答案:

答案 0 :(得分:2)

您可以使用

a[a$ui %in% user_id, ]

例如,

> a <- data.frame(ui = c(237, 261, 290), v1 = c(1, 2, 3), v2 = c(2, 3, 4))
> a
   ui v1 v2
1 237  1  2
2 261  2  3
3 290  3  4
> user_id <- 261
> a[a$ui %in% user_id, ]
   ui v1 v2
2 261  2  3
> user_id <- c(261, 237)
> a[a$ui %in% user_id, ]
   ui v1 v2
1 237  1  2
2 261  2  3

答案 1 :(得分:0)

不应该

 Fragment fragmentStartPage = null;
                fragmentStartPage = new FragmentEmails();
                FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                fragmentTransaction.replace(R.id.email_container, fragmentStartPage);
                fragmentTransaction.commit();

最后没有这个[i]? 然后:

user_id=actions_slippers$ui