如何在另一个向量中找到向量元素的数量?

时间:2017-10-20 00:14:59

标签: r vector

我有两个向量。第一个矢量名称为comments$author_id,第二个为enrolments$learner_id。我想在enrolmens数据框中添加新列,以显示每个comments$author_id行的enrolment$learner_id向量中重复行数。

示例:

 if(enrolments$learner_id[1] repeated 5 times in comments$author_id)
    enrolments$freqs[1] = 5

我可以不使用任何循环吗?

载体样品如下:

df1 <- data.frame(v1 = c(1,1,1,4,5,5,4,1,2,3,5,6,2,1,5,2,3,4,1,6,4,2,3,5,1,2,5,4))
df2 <- data.frame(v2 = c(1,2,3,4,5,6))

我想添加&#34;计数&#34;列到&#34; df2&#34;它显示了v1中重复的v2元素的计数。

3 个答案:

答案 0 :(得分:1)

使用您的df1df2示例,您可以这样做:

# Make data
df1 = data.frame(v1 = c(1,1,1,4,5,5,4,1,2,3,5,6,2,1,5,2,3,4,1,6,4,2,3,5,1,2,5,4))
df2 = data.frame(v2 = c(1,2,3,4,5,6))

# Add 'count' variable as reqeuested
df2$counts = sapply(df2$v2, function(x) {
  sum(df1$v1 == x, na.rm = T) #na.rm=T just in case df1$v1 has missing values
})
df2 #view output

答案 1 :(得分:1)

您实际上在做的是聚合df1以获取计数,然后将此计数添加回df2集。这个逻辑可以很容易地转换成一堆不同的方法:

# base R
merge(
  df2,
  aggregate(cbind(df1[0], count=1), df1["v1"], FUN=sum),
  by.x="v2", by.y="v1", all.x=TRUE
)

# data.table
library(data.table)
setDT(df1)
setDT(df2)
df2[df1[, .(count=.N), by=v1], on=c("v2"="v1")]

# dplyr
library(dplyr)
df1 %>%
  group_by(v1) %>%
  count() %>%
  left_join(df2, ., by=c("v2"="v1"))

#  v2 count
#1  1     7
#2  2     5
#3  3     3
#4  4     5
#5  5     6
#6  6     2

答案 2 :(得分:1)

  

&#34; [tabulate]给我这个错误:$&lt; - 。data.frame( tmp ,&#34; comments_count&#34;,value = c(0L, 0L,:替换有25596行数据   有25597&#34;

这很谨慎,因为<form name="userForm"> <div class="header-buttons3"> <button type="button" class="header-button-save btn" ng-disabled="userForm.$invalid" ng-click="submitUserForm()"><i class="fa fa-pencil"></i>SAVE</button> <button type="button" class="header-button-cancel"><i class="fa fa-chevron-left"></i>CANCEL</button> </div> <div class="clearfix"></div> <div class="row"> <div class="col-md-11 col-sm-12 col-xs-12"> <div class="x_content"> <br> <div class="form-row row"> <div class="form-group required col-md-3"> <label for="first name" class="control-label">FIRST NAME</label> <span class="after">*</span> <input class="form-control" ng-class="{validbr: showfn==='false', invalidbr: showfn }" ng-focus="showfn=false" ng-blur="showfn=userForm.firstName.$invalid" id="firstname" class="form-control" ng-model="user.firstName" ng-pattern="/^[a-zA-Z]*$/" name="firstName" ng-maxlength="30" maxlength="30" required placeholder="Enter First Name" /> <div style="color: Red" ng-show="showfn && userForm.firstName.$error.required">Error - First Name Required</div> <div style="color: Red" ng-show="showfn && userForm.firstName.$error.pattern">Error - Enter only Character</div> <div style="color: Red" ng-show="showfn && userForm.firstName.$error.maxlength">Error - Need 30 char max</div> </div> <div class="form-group required col-md-3"> <label for="last name" class="control-label">LAST NAME</label> <span class="after">*</span> <input class="form-control" ng-class="{validbr: showln==='false', invalidbr: showln }" ng-focus="showln=false" ng-blur="showln=userForm.lastName.$invalid" id="lastname" ng-model="user.lastName" ng-pattern="/^[a-zA-Z]*$/" name="lastName" ng-maxlength="50" maxlength="50" required placeholder="Enter Last Name" /> <div style="color: Red" ng-show="showln && userForm.lastName.$error.required">Error - Last Name Required</div> <div style="color: Red" ng-show="showln && userForm.lastName.$error.pattern">Error - Enter only Character</div> <div style="color: Red" ng-show="showln && userForm.lastName.$error.maxlength">Error - Need 50 char max</div> </div> <div class="form-group required col-md-3"> <label for="inputEmail4" class="control-label">EMAIL ADDRESS</label> <span class="after">*</span> <input type="email" readonly class="form-control" ng-class="{validbr: showem==='false', invalidbr: showem }" ng-focus="showem=false" ng-blur="showem=userForm.emailId.$invalid" id="emailId" ng-model="user.emailId" name="emailId" ng-maxlength="70" maxlength="70" required placeholder="Enter Email Address" /> <div style="color: Red" ng-show="showem && userForm.emailId.$error.required">Error - Email address Required</div> <div style="color: Red" ng-show="showem && userForm.emailId.$error.email">Error - Not a valid email address</div> </div> <div class="form-group required col-md-3"> <label for="inputEmail4" class="control-label">USER STATUS </label><br/> <label> <input type="radio" ng-model="user.active" ng-value="false"> IN ACTIVE </label> <label> <input type="radio" ng-model="user.active" ng-value="true"> ACTIVE </label> </div> </div> <div class="form-row row"> <div class="form-group col-md-3"> <label for="mobile phone" class="col-form-label">MOBILE PHONE</label> <input class="form-control" id="mobileNumber" ng-class="{validbr: showmn==='false', invalidbr: showmn }" ng-focus="showmn=false" ng-blur="showmn=userForm.mobileNumber.$invalid" ng-model="user.mobileNumber" name="mobileNumber" ng-minlength="14" ng-maxlength="14" phone-input placeholder="Enter Mobile Phone #" /> <div style="color: Red" ng-show="showmn && userForm.mobileNumber.$error.minlength ">Error - Need 10 digits for a Phone #</div> <div style="color: Red" ng-show="showmn && userForm.mobileNumber.$error.maxlength ">Error - Need 10 digits for a Phone #</div> <!-- <div ng-show="userForm.mobileNumber.$error.minlength">Error - Need 10 digits for a Phone #</div> --> </div> <div class="form-group col-md-3"> <label for="Desk Phone" class="col-form-label">DESK PHONE</label> <input class="form-control" id="deskNumber" ng-class="{validbr: showdn==='false', invalidbr: showdn }" ng-focus="showdn=false" ng-blur="showdn=userForm.deskNumber.$invalid" name="deskNumber" ng-model="user.deskNumber" ng-minlength="14" maxlength="14" phone-input placeholder="Enter Desk Phone #" /> <div style="color: Red" ng-show="showdn && userForm.deskNumber.$error.minlength">Error - Need 10 digits for a Phone #</div> <div style="color: Red" ng-show="showdn && userForm.deskNumber.$error.maxlength">Error - Need 10 digits for a Phone #</div> </div> <div class="form-group"> <!-- <input type ="hidden" ng-model="user.createdBy" name="createdBy" /> <input type ="hidden" ng-model="user.updatedBy" name="updatedBy" /> --> </div> <!-- <input type="hidden" ng-value="user.createdBy" name="createdBy" /> <input type="hidden" ng-value="user.updatedBy" name="updatedBy" /> --> </div> </div> </div> </div> </form> 末尾有一个值,而不是df2$v2的一部分 - 我将df1$v10添加到您的示例中以显示的是:

7

要使用df1 <- data.frame(v1 = c(1,1,1,4,5,5,4,1,2,3,5,6,2,1,5,2,3,4,1,6,4,2,3,5,1,2,5,4)) df2 <- data.frame(v2 = c(1,2,3,0,4,5,6,7)) df2$count <- tabulate(factor(df1$v1, df2$v2)) # Error in `$<-.data.frame`(`*tmp*`, count, value = c(7L, 5L, 3L, 0L, 5L, : # replacement has 7 rows, data has 8 进行更正,这可能是较大数据的最快解决方案:

tabulate

有关该功能的文档,请参阅df2$count <- tabulate(factor(df1$v1, df2$v2), length(df2$v2)) df2 # v2 count # 1 1 7 # 2 2 5 # 3 3 3 # 4 0 0 # 5 4 5 # 6 5 6 # 7 6 2 # 8 7 0