Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/svg/2.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
在数据帧中查找行并更改单元格中的值-Rstudio_R_Dataframe_Data Analysis - Fatal编程技术网

在数据帧中查找行并更改单元格中的值-Rstudio

在数据帧中查找行并更改单元格中的值-Rstudio,r,dataframe,data-analysis,R,Dataframe,Data Analysis,我对R中的脚本有问题。我有一个数据帧(CSV),每个文件中有几百行。我想与文件(较新的和较旧的)进行比较。在旧版本中,有某些值,如国家、客户ID、类别和代表ID。每个客户都有一个客户。有时客户会改变。并且更改在每个文件中。所以最早的文件(dataframe)是基本数据库,在下面的文件中,只有更改。让我们说: PL|Pharmacy|WPLF1111123233|F111 其中WPLF1111123233是CustomerID,F112是RepresentativeID 在下一个数据帧中有 P

我对R中的脚本有问题。我有一个数据帧(CSV),每个文件中有几百行。我想与文件(较新的和较旧的)进行比较。在旧版本中,有某些值,如国家、客户ID、类别和代表ID。每个客户都有一个客户。有时客户会改变。并且更改在每个文件中。所以最早的文件(dataframe)是基本数据库,在下面的文件中,只有更改。让我们说:

PL|Pharmacy|WPLF1111123233|F111 
其中WPLF1111123233是CustomerID,F112是RepresentativeID

在下一个数据帧中有

PL|Pharmacy|WPLF1111123233|F333
这和上面一样,但同一代表已从F111改为F333

所以我试着用这样的公式。要在第一个数据帧和第二个数据帧中检查客户机ID,如果在第二个数据帧中存在客户机ID,则意味着代表已更改,因此在第一个文件中我也应更改它

dataset$RepID <- ifelse ((dataset$CustomerID %in% dataset2$CustomerID), dataset2$RepID, dataset$RepID)
变化:

D|PL|Terytorium_nowe|WPLF07253186|17|Grandpa|1
D|PL|Terytorium_nowe|WPLF00003224|25|Pontiac|1
D|PL|Terytorium_nowe|WPLF02818905|26|Catalina|1
I|PL|Terytorium_nowe|WPLF02615545|20|Chicago|1
I|PL|Terytorium_nowe|WPLF02615557|01|Foxtrot|1
I|PL|Terytorium_nowe|WPLF02615553|36|Tango|1
I|PL|Terytorium_nowe|WPLF02615561|36|Hollywood|1
I|PL|Terytorium_nowe|WPLF02615564|31|Zulu|1 

因此,DataToCheck中的列表应该更新->名称应该从
F(数字)更改不应触摸或更改DataToCheck中的
到北约字母表以及ID为WPLF00007731和ID为F132的行,应将更改为的行添加到第一个DataToCheck

具有基本循环方式的解决方案。(根据数据帧更改列名。)

这里我假设
OLD_DATA_TO_CHECK
是您的旧数据,需要根据需求进行修改

for(i in 1:length(OLD_DATA_TO_CHECK$Customer_Id)){
  for(j in 1:length(CHANGES$Customer_Id)){
    if(OLD_DATA_TO_CHECK$Customer_Id[i] != 'WPLF00007731' & OLD_DATA_TO_CHECK$Col6[i] != "F132"){  # Checking for the row which shold not modify
      if(OLD_DATA_TO_CHECK$Customer_Id[i] == CHANGES$Customer_Id[j]){
        OLD_DATA_TO_CHECK$Col6[i] <- CHANGES$Col6[j]
        if(CHANGES$Col6[i] == 'Grandpa'){
         OLD_DATA_TO_CHECK <- rbind(CHANGES[i,], OLD_DATA_TO_CHECK) # binding the identified row from CHANGES to top row of OLD_DATA_TO_CHECK 
       } 
    }
  }
  }
}

OLD_DATA_TO_CHECK #after modifications

  Col1  Country Category        Customer_Id   Col5 Col6       Col7
1 D     PL      Terytorium_nowe WPLF07253186    17 Grandpa       1
2 D     PL      Terytorium_nowe WPLF00003224    25 Pontiac       1
3 D     PL      Terytorium_nowe WPLF02818905    26 Catalina      1
4 I     PL      Terytorium_nowe WPLF02615545    20 Chicago       1
5 I     PL      Terytorium_nowe WPLF02615553    36 Tango         1
6 I     PL      Terytorium_nowe WPLF02615557     1 Foxtrot       1
7 I     PL      Terytorium_nowe WPLF02615561    36 Hollywood     1
8 I     PL      Terytorium_nowe WPLF02615564    31 Zulu          1
9 I     PL      Terytorium_nowe WPLF00007731    34 F132          1
for(1中的i:长度(旧数据到支票$Customer\u Id)){
用于(j in 1:长度(更改$Customer_Id)){
if(OLD_DATA_TO_CHECK$Customer_Id[i]!=“WPLF00007731”&OLD_DATA_TO_CHECK$Col6[i]!=“F132”){检查不应修改的行
如果(旧数据到检查$Customer\u Id[i]==更改$Customer\u Id[j]){

OLD_DATA_TO_CHECK$Col6[i]如果您可以提供数据帧的样本数据进行尝试,这将非常有用。@如果数据是可复制的(使用dput或类似工具),则使用数据编辑的msr_003将非常有用。谢谢你的回答!谢谢你的帮助。这不完全是我的意思,但我的灵感使我解决了这个问题。早些时候,我只是认为我可以只用向量来解决这个问题,但问题是数据中的行顺序。现在一切都很好
OLD_DATA_TO_CHECK 

   Col1  Country Category        Customer_Id   Col5 Col6   Col7
1 D     PL      Terytorium_nowe WPLF00003224    25 F123      1
2 D     PL      Terytorium_nowe WPLF02818905    26 F120      1
3 I     PL      Terytorium_nowe WPLF02615545    20 F110      1
4 I     PL      Terytorium_nowe WPLF02615553    36 F112      1
5 I     PL      Terytorium_nowe WPLF02615557     1 F106      1
6 I     PL      Terytorium_nowe WPLF02615561    36 F112      1
7 I     PL      Terytorium_nowe WPLF02615564    31 F101      1
8 I     PL      Terytorium_nowe WPLF00007731    34 F132      1 


CHANGES

    Col1  Country Category        Customer_Id   Col5 Col6       Col7
1 D     PL      Terytorium_nowe WPLF07253186    17 Grandpa       1
2 D     PL      Terytorium_nowe WPLF00003224    25 Pontiac       1
3 D     PL      Terytorium_nowe WPLF02818905    26 Catalina      1
4 I     PL      Terytorium_nowe WPLF02615545    20 Chicago       1
5 I     PL      Terytorium_nowe WPLF02615557     1 Foxtrot       1
6 I     PL      Terytorium_nowe WPLF02615553    36 Tango         1
7 I     PL      Terytorium_nowe WPLF02615561    36 Hollywood     1
8 I     PL      Terytorium_nowe WPLF02615564    31 Zulu          1
for(i in 1:length(OLD_DATA_TO_CHECK$Customer_Id)){
  for(j in 1:length(CHANGES$Customer_Id)){
    if(OLD_DATA_TO_CHECK$Customer_Id[i] != 'WPLF00007731' & OLD_DATA_TO_CHECK$Col6[i] != "F132"){  # Checking for the row which shold not modify
      if(OLD_DATA_TO_CHECK$Customer_Id[i] == CHANGES$Customer_Id[j]){
        OLD_DATA_TO_CHECK$Col6[i] <- CHANGES$Col6[j]
        if(CHANGES$Col6[i] == 'Grandpa'){
         OLD_DATA_TO_CHECK <- rbind(CHANGES[i,], OLD_DATA_TO_CHECK) # binding the identified row from CHANGES to top row of OLD_DATA_TO_CHECK 
       } 
    }
  }
  }
}

OLD_DATA_TO_CHECK #after modifications

  Col1  Country Category        Customer_Id   Col5 Col6       Col7
1 D     PL      Terytorium_nowe WPLF07253186    17 Grandpa       1
2 D     PL      Terytorium_nowe WPLF00003224    25 Pontiac       1
3 D     PL      Terytorium_nowe WPLF02818905    26 Catalina      1
4 I     PL      Terytorium_nowe WPLF02615545    20 Chicago       1
5 I     PL      Terytorium_nowe WPLF02615553    36 Tango         1
6 I     PL      Terytorium_nowe WPLF02615557     1 Foxtrot       1
7 I     PL      Terytorium_nowe WPLF02615561    36 Hollywood     1
8 I     PL      Terytorium_nowe WPLF02615564    31 Zulu          1
9 I     PL      Terytorium_nowe WPLF00007731    34 F132          1