我有以下数据框(df1):
C12 C34
C1 C2 C3 C4
R1 11 21 31 a1
R2 de bc cc b
R3 dc ec dc c
R4 gc gc gc d
我可以将每个2个名称的列名添加到一起并获取以下数据框(df2):
C12
C1 C2
R1 11 21
R2 de bc
R3 dc ec
R4 gc gc
应添加C12和C34,以便我可以获得df2(C12)的子集:
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Genealogical {
private static List<Person> persons = new ArrayList<Person>();
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
while(true)
{
String firstLine = input.nextLine();
String[] splitted = firstLine.split(" : ");
if(splitted.length == 0)
{
System.exit(0);
}
if(firstLine.contains("BIRTH"))
{
String childName = splitted[0].substring(6);
birth(childName, splitted[1], splitted[2], splitted[3]);
}
else if(firstLine.contains("DEATH"))
{
if(!firstLine.contains(" : "))
{
if(persons.size() > 0)
persons.get(persons.size() - 1).kill(firstLine.substring(6));
}
else
{
String name = splitted[0].substring(6);
getPerson(name).kill(splitted[1]);
}
}
else if(firstLine.contains("ANCESTORS"))
{
String name = splitted[0].substring(10);
Person ancestor = getPerson(name);
for(Person p : persons)
{
if(p.getName().equals(name) || p.used)
{
continue;
}
else
{
p.used = true;
ancestor.addAncestors(p);
}
}
}
else if(firstLine.contains("DESCENDANTS"))
{
String name = splitted[0].substring(12);
Person descendant = getPerson(name);
for(Person p : persons)
{
if(p.getName().equals(name) || p.used)
{
continue;
}
else
{
p.used = true;
descendant.addDescendants(p);
}
}
}
else if(firstLine.contains("QUIT"))
{
if(persons.size() > 0)
{
for(int i = persons.size() - 1; i >= 0; i--)
{
Person p = persons.get(i);
if(p.getAncestors().size() > 0)
{
printAncestor(p);
}
if(p.getDescendants().size() > 0)
{
printDescendant(p);
}
}
}
System.exit(0);
}
}
}
public static void printAncestor(Person p)
{
System.out.println("ANCESTORS of " + p.getName());
for(Person ancestor : p.getAncestors())
{
System.out.println(" " + ancestor.getName() + " " + ancestor.getDate() + " -" + ancestor.getDeathdate());
System.out.println(" " + ancestor.getDad().getName());
System.out.println(" " + ancestor.getMom().getName());
}
System.out.println();
}
public static void printDescendant(Person p)
{
System.out.println("DESCENDANTS of " + p.getName());
for(Person descendant : p.getDescendants())
{
System.out.println(" " + descendant.getName() + " " + descendant.getDate() + " -" + descendant.getDeathdate());
}
}
private static void birth(String child, String date, String mother, String father)
{
Person mom = getPerson(mother);
if(mom == null)
{
mom = new Person(null, null);
mom.setName(mother);
}
Person dad = getPerson(father);
if(dad == null)
{
dad = new Person(null, null);
dad.setName(father);
}
Person childd = new Person(mom, dad);
childd.setName(child);
childd.setDate(date);
persons.add(childd);
}
private static Person getPerson(String person)
{
for(Person p : persons)
{
if(p.getName().equals(person))
{
return p;
}
}
return null;
}
}
答案 0 :(得分:4)
我不知道如何将第二组列名添加到数据框中。话虽如此,您可以通过创建包含所需列名称的向量来轻松实现此功能:
C12 <- c("C1", "C2")
C34 <- c("C3", "C4")
df1[C12]
# C1 C2
# R1 11 21
# R2 de bc
# R3 dc ec
# R4 gc gc
df1[C34]
# C3 C4
# R1 31 a1
# R2 cc b
# R3 dc c
# R4 gc d
如果您想要按行进行子集化,则可以使用行名称:
R12 <- c("R1", "R2")
R34 <- c("R3", "R4")
df1[R12,]
# C1 C2 C3 C4
# R1 11 21 31 a1
# R2 de bc cc b
df1[R34,]
# C1 C2 C3 C4
# R3 dc ec dc c
# R4 gc gc gc d