我有一个这样的数据框:
@Test
public void testguardarMensaje() throws IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException, InstantiationException{
String datoTest = "1=123456";
Constructor<PsPort> constructor = PsPort.class.getDeclaredConstructor(new Class[] {String.class});
constructor.setAccessible(true);
PsPort port = constructor.newInstance("middleware.conf");
PsPort drMock;
int hash1 = datoTest.hashCode();
String hashString1 = String.valueOf(hash1);
String combinedIdDataHashString1 = datoTest +"="+ hashString1;
drMock = strictMock(PsPort.class);
byte[] datoByte = port.encriptarDesencriptarMensaje(combinedIdDataHashString1.getBytes(), Cipher.ENCRYPT_MODE);
drMock.guardarDato(datoByte);
replayAll();
int hash = datoTest.hashCode();
String hashString = String.valueOf(hash);
String combinedIdDataHashString = datoTest +"="+ hashString;
byte[] datoByte2 = port.encriptarDesencriptarMensaje(combinedIdDataHashString.getBytes(), Cipher.ENCRYPT_MODE);
DatagramPacket paquete = new DatagramPacket(datoByte2,datoByte2.length);
paquete.getData();
DataReader dr = new DataReader(port, null, 100, "=", "C:/Users/Asier/Desktop/logs/");
dr.guardarMensaje(paquete, port);
verifyAll();
}
我想获得一个包含所有具有相同值的ID的新表,如下所示:
ID Value
1 C
2 B
3 B
4 A
5 C
6 B
7 B
8 A
9 C
10 B
11 B
12 A
13 C
14 B
15 B
我不介意昏迷是否分开。
由于
答案 0 :(得分:2)
对于每个独特的“价值”,我们可以使用aggregate
到paste
'ID'
aggregate(ID~Value, df1, FUN= toString)
# Value ID
#1 A 4, 8, 12
#2 B 2, 3, 6, 7, 10, 11, 14, 15
#3 C 1, 5, 9, 13
如果我们需要list
列,请使用c
作为FUN
aggregate(ID~Value, df1, FUN = c)
使用其他软件包(例如data.table
library(data.table)
setDT(df1)[, .(Values =toString(Value)), by = ID]
或dplyr
library(dplyr)
df1 %>%
group_by(ID) %>%
summarise(Values = toString(Value))