r语言中如何统计满足制定条件的行数?

Python015

r语言中如何统计满足制定条件的行数?,第1张

第一个用table(), 会把所有user的频数显示出来

table(testing$user)

第二个就取交集算个数吧。

如果只看user == 2中,item也相同的个数,那么

testuser <- testing[testing$user == 2, ]

trainuser <- trainuser[training$user == 2, ]

length(intersect(testuser, trainuser))

r语言dim函数怎么用计算数组的行数和列数 dim

POST方式 方式传递参数

//和GET方式一样,先将参数放入List

params = new LinkedList<BasicNameValuePair>()

params.add(new BasicNameValuePair("param1", "Post方法"))//增加参数1

params.add(new BasicNameValuePair("param2", "第二个参数"))//增加参数2

try {

HttpPost postMethod = new HttpPost(baseUrl)//创建一个post请求

postMethod.setEntity(new UrlEncodedFormEntity(params, "utf-8"))//将参数填入POST Entity中

HttpResponse response = httpClient.execute(postMethod)//执行POST方法

Log.i(TAG, "resCode = " + response.getStatusLine().getStatusCode())//获取响应码

Log.i(TAG, "result = " + EntityUtils.toString(response.getEntity(), "utf-8"))//获取响应内容

} catch (UnsupportedEncodingException e) {

e.printStackTrace()

} catch (ClientProtocolException e) {

e.printStackTrace()

} catch (IOException e) {

e.printStackTrace()

}

其基本思想就是把百分数按照字符处理,首先将“%”与数字分离,然后再将数除以100,就可以化成小数了。下面两种方法的区别一个是将%替换成空格,一个是提取除百分号的数字。>testdata<-data.frame(v1=c("78%", "65%", "32%"), v2=c("43%", "56%", "23%"))>testnewdata1<-data.frame(lapply(testdata, function(x) as.numeric(sub("%", "", x))/100) )>testnewdata1v1 v21 0.78 0.432 0.65 0.563 0.32 0.23>library(stringr)>testnewdata2<-data.frame(lapply(testdata, function(x) as.numeric(str_extract(x,'[0-9.]+'))/100) )>testnewdata2v1 v21 0.78 0.432 0.65 0.563 0.32 0.23 替换百分号的思想还可以用下面的代码实现>testnewdata3<-data.frame(lapply(testdata, function(x) as.numeric(gsub("\\%", "", x))/100))>testnewdata3v1 v21 0.78 0.432 0.65 0.563 0.32 0.23