使用R studio精炼数据集

时间:2016-08-10 22:26:59

标签: r excel functional-programming logic

SampleData.xlsx

A1001型A1002 A1003 A1004 A1005总计 类型A

的TypeB TypeC
键入
总计

我必须使用R编程来优化上述形式的数据集。寻找IDEA来计算每种类型的通信w.r.t UserID

     *

data11 <- read.xlsx("SampleData.xlsx", sheetName = "SampleData1", header = TRUE)
data22 <- read.xlsx("SampleData.xlsx", sheetName = "SampleData2", header = TRUE)
dim(data11)
dim(data22)
data2 = data22[complete.cases(data22),]
data2$User <- as.character(data2$User)
data1$User = NA
for(i in data1[,1])
{
  for(q in data2[,1])
  {
      if(data1$Id[i] == data2$Id[q])
      {
  data1$User[i] = data2$User[q]
      }
  }
}
data.1 = data1[complete.cases(data1),]
 x1 <- subset(data.1, Type == "Email" )
 x2 <- subset(data.1, Type == "Update" )
x3 <- subset(data.1, Type == "NA" )
x4 <- subset(data.1, Type == "Call" )
x5 <- subset(data.1, Type == "Visit" )
p1 = as.list(as.data.frame(count(x1, "User")))
Type1 = p1$freq

*

从最后两行CODE可以看出。我尝试了一些东西,但这是错误的方式。

帮帮我

2 个答案:

答案 0 :(得分:0)

如果您首先更正了缺失的代码行:

$_POST["asunto"];
$_POST["mail"];

然后,您可以合并并运行表。 (我的R工作区中没有&#34;计数&#34;功能。)

 data1 = data11[complete.cases(data11),]

答案 1 :(得分:0)

这是一个R Markdown文档。

library(xlsx)

data11 <- read.xlsx("SampleData.xlsx", sheetName = "SampleData1", header = TRUE)
data22 <- read.xlsx("SampleData.xlsx", sheetName = "SampleData2", header = TRUE)


    data2 = data22[complete.cases(data22),]
    data1 = data11[complete.cases(data11),]
    data1$User = NA

    data2$User <- as.character(data2$User)

    for(i in data1[,1])
{
  for(q in data2[,1])
  {
      if(data1$Id[i] == data2$Id[q])
      {
  data1$User[i] = data2$User[q]
      }
  }
}

data.1 = data1[complete.cases(data1),]
summary(data1)
```

```{r, echo=FALSE}
 x1 <- subset(data.1, Type == "Email" )
 x2 <- subset(data.1, Type == "Update" )
x3 <- subset(data.1, Type == "NA" )
x4 <- subset(data.1, Type == "Call" )
x5 <- subset(data.1, Type == "Visit" )
```

```{r}
 newd <- table(data1$Type, data1$User)
 net = as.data.frame.matrix(newd)
 net$Total = net$A1001 + net$A1002 + net$A1003 + net$A1004 + net$A1005

 print(net)

```