简体   繁体   中英

How can I get the whole row when I give a column's element as input in R?

this is my dataset:

 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] this user id coming from another dataframe

I want to get the user_id's row and I have tried the following code:

 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, ]

my goal is here to get user_id's vector from a dataframe and then calculate the cosine similarity between user_id vector and other vectors in a dataframe

but it returned:

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

Can anyone tell me how to do this?

You can use

a[a$ui %in% user_id, ]

For example,

> 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

Shouldn't it be

user_id=actions_slippers$ui

without this [i] at the end? And then:

a[a$ui%in%user_id, ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM