Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用R中有两个变量的循环更新数据帧_R_Dataframe - Fatal编程技术网

使用R中有两个变量的循环更新数据帧

使用R中有两个变量的循环更新数据帧,r,dataframe,R,Dataframe,我对R比较陌生,希望开发一个循环,允许我使用两个变量更新数据帧中的列 我有两个主要的数据帧-BaseData和CountyFile(两个示例-主文件大约有300万行) BaseData是来源和目的地列表,其中变量(1、2、3、4等)表示县的id。因此,对于userid2,源是country 1,目标是country 2,依此类推 BaseData UserID Origin Destination 1 1 1 1 2 2

我对R比较陌生,希望开发一个循环,允许我使用两个变量更新数据帧中的列

我有两个主要的数据帧-BaseData和CountyFile(两个示例-主文件大约有300万行)

BaseData是来源和目的地列表,其中变量(1、2、3、4等)表示县的id。因此,对于userid2,源是country 1,目标是country 2,依此类推

BaseData
     UserID Origin Destination
1       1      1           1
2       2      2           1
3       3      3           2
4       4      4           4
5       5      1           2
6       6      1           3
CountyFile是一个数据帧,它将包含目标县(county_ID)和所有源县(C_1、C_2等)之间所有交互的总和

我可以通过创建一个子集BaseData(其中Destination==1)来获得所需的信息,对原始数据进行分组和求和,然后更新CountyFile.C_1

Temp1 <- subset(BaseData, Destination  == 1) 
Temp2 <- as.data.frame(table(Temp1$Origin))
CountyFile$C_1<-Temp2[match(CountyFile$CountyID, Temp2$Var1),2]
我在下面使用了一个嵌套循环和两个变量(I和j),但都没有用。也许有人能提供一个更简单的解决方案

for (i in c(01,02,03,04,05,06,07,08,09,10,11,12,13,14,15)) 
{
Temp1 <- subset(BaseData, Destination  == i) 
Temp2 <- as.data.frame(table(Temp1$Origin)) }
for (j in c("C_1","C_2","C_3","C_4","C_5","C_6","C_7")) 
{
CountyFile$j<-Temp2[match(CountyFile$CountyID, Temp2$Var1),2]
}
for(i在c中(01,02,03,04,05,06,07,08,09,10,11,12,13,14,15))
{

Temp1在R中操作数据有更简单的方法。下面是使用
数据的一种方法。table

library(data.table)
# the actual code starts
setDT(BaseData)
# count the number of rows in each Destination, Origin combinations
CountData <- BaseData[, .N, by = .(Destination, Origin)]
# reshape the data
OutputData <- dcast(CountData, Destination ~ Origin)
# rename the columns
names(OutputData) <- c("CountyID", 
                       paste0("C_", 1:7))

太棒了。这个解决方案非常有效。感谢您的帮助!
for (i in c(01,02,03,04,05,06,07,08,09,10,11,12,13,14,15)) 
{
Temp1 <- subset(BaseData, Destination  == i) 
Temp2 <- as.data.frame(table(Temp1$Origin)) }
for (j in c("C_1","C_2","C_3","C_4","C_5","C_6","C_7")) 
{
CountyFile$j<-Temp2[match(CountyFile$CountyID, Temp2$Var1),2]
}
library(data.table)
# the actual code starts
setDT(BaseData)
# count the number of rows in each Destination, Origin combinations
CountData <- BaseData[, .N, by = .(Destination, Origin)]
# reshape the data
OutputData <- dcast(CountData, Destination ~ Origin)
# rename the columns
names(OutputData) <- c("CountyID", 
                       paste0("C_", 1:7))
# generate example data
N <- 500
BaseData <- data.frame(UserId = seq(N),
                       Destination = sample(15, N, TRUE),
                       Origin = sample(7, N, TRUE))
head(OutputData)
##    CountyID C_1 C_2 C_3 C_4 C_5 C_6 C_7
## 1:        1   1   1   5   5   5   7   7
## 2:        2  10   4   6   4   3   8   3
## 3:        3   5   4   6   3   4   6   6
## 4:        4   9   4   1   3   2   5   4
## 5:        5   7   2  10   8   4   1   7
## 6:        6   6   5   4   4   4   6   5