在R中按列合并数字和字符?

时间:2017-12-21 13:51:31

标签: r dataframe

我有2个df'

--force-rm

我想把它结合起来:

df1 <- data.frame(m = c("m1","m2","m3"), score = c(2,3,3))
df2 <- data.frame(m = c("m3", "m2", "m1"), dosage = c("a", "b", "c"))

我尝试过使用粘贴命令,但是我没有使用colnames。 我应该使用连接还是有其他有效方法吗?

3 个答案:

答案 0 :(得分:2)

我们可以通过“m”列执行merge(如果有常见的列名称,它会在by参数中提到它而不提及它),然后transform通过paste'得分'和'剂量'创建“新列”,通过索引对列进行分组

transform(merge(df1, df2), newcolumn = paste0(score, dosage))[c(1,4)]

如果我们需要名为vector

dfN <- merge(df1, df2)
setNames(do.call(paste0, dfN[-1]), dfN[,1])
# m1   m2   m3 
#"2c" "3b" "3a" 

答案 1 :(得分:1)

使用public class IngredientsExpandableList extends ExpandableListActivity { // Create ArrayList to hold parent Items and Child Items private ArrayList<ParentModel> parentItems = new ArrayList<ParentModel>(); private ArrayList<ChildModel> childItems = new ArrayList<ChildModel>(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Create Expandable List and set it's properties ExpandableListView expandableList = getExpandableListView(); expandableList.setDividerHeight(0); expandableList.setGroupIndicator(null); expandableList.setClickable(true); //Setting the data. setData(); // Create the Adapter MyExpandableAdapter adapter = new MyExpandableAdapter(parentItems, childItems); adapter.setInflater(LayoutInflater.from(this), this); // Set the Adapter to expandableList expandableList.setAdapter(adapter); expandableList.setOnChildClickListener(this); } public void setData(){ String[] colors = getResources().getStringArray(R.array.ingredientsColor); String[] innerColors = getResources().getStringArray(R.array.ingredientsInnerColor); // Set the Items of Parent for (int i = 0; i < 10; i++){ parentItems.add(new ParentModel("ingredients "+(i+1), Color.parseColor(colors[i]))); } // Set The Child Data ArrayList<String> child = null; for (int i = 0; i < parentItems.size(); i++){ child = new ArrayList<String>(); for (int k = 0; k < 4; k++){ child.add("ingredient " + (k+1)); } childItems.add(new ChildModel(child,Color.parseColor(innerColors[i]))); } } public void makeToast(String string){ Toast.makeText(this, string, Toast.LENGTH_SHORT).show(); } public class ParentModel{ String text; int color; public ParentModel(String text, int color) { this.text = text; this.color = color; } public String getText() { return text; } public int getColor() { return color; } } public class ChildModel{ ArrayList<String> text; int color; ArrayList<Boolean> checked = new ArrayList();; public ChildModel(ArrayList<String> text, int color) { this.text = text; this.color = color; for(String i:text) { checked.add(false); } } public ArrayList<String> getText() { return text; } public int getColor() { return color; } } public class MyExpandableAdapter extends BaseExpandableListAdapter { private Activity activity; private ArrayList<ChildModel> childItems; private LayoutInflater inflater; private ArrayList<ParentModel> parentItems; private ArrayList<String> child; // constructor public MyExpandableAdapter(ArrayList<ParentModel> parents, ArrayList<ChildModel> childern) { this.parentItems = parents; this.childItems = childern; } public void setInflater(LayoutInflater inflater, Activity activity) { this.inflater = inflater; this.activity = activity; } // method getChildView is called automatically for each child view. // Implement this method as per your requirement @Override public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { child = childItems.get(groupPosition).getText(); TextView textView = null; CheckBox checkBox = null; LinearLayout LinearView = null; if (convertView == null) { convertView = inflater.inflate(R.layout.child_list, null); } // get the textView reference and set the value textView = (TextView) convertView.findViewById(R.id.textView1); textView.setText(child.get(childPosition)); LinearView = (LinearLayout) convertView.findViewById(R.id.layout); LinearView.setBackgroundColor(childItems.get(groupPosition).getColor()); checkBox = (CheckBox) convertView.findViewById(R.id.checkBox1); checkBox.setChecked(childItems.get(groupPosition).checked.get(childPosition)); checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { childItems.get(groupPosition).checked.get(childPosition) = isChecked; } }); // set the ClickListener to handle the click event on child item convertView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { makeToast("clicked"); } }); return convertView; } // method getGroupView is called automatically for each parent item // Implement this method as per your requirement @Override public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { if (convertView == null) { convertView = inflater.inflate(R.layout.parent_list, null); } ((CheckedTextView) convertView.findViewById(R.id.textViewGroup)).setText(parentItems.get(groupPosition).getText()); ((CheckedTextView) convertView.findViewById(R.id.textViewGroup)).setBackgroundColor(parentItems.get(groupPosition).getColor()); ((CheckedTextView) convertView.findViewById(R.id.textViewGroup)).setChecked(isExpanded); return convertView; } @Override public Object getChild(int groupPosition, int childPosition) { return null; } @Override public long getChildId(int groupPosition, int childPosition) { return 0; } @Override public int getChildrenCount(int groupPosition) { return childItems.get(groupPosition).getText().size(); } @Override public Object getGroup(int groupPosition) { return null; } @Override public int getGroupCount() { return parentItems.size(); } @Override public void onGroupCollapsed(int groupPosition) { super.onGroupCollapsed(groupPosition); } @Override public void onGroupExpanded(int groupPosition) { super.onGroupExpanded(groupPosition); } @Override public long getGroupId(int groupPosition) { return 0; } @Override public boolean hasStableIds() { return false; } @Override public boolean isChildSelectable(int groupPosition, int childPosition) { return false; } } dplyr

的解决方案
tidyr

这假设所有df1 <- data.frame(m = c("m1","m2","m3"), score = c(2,3,3)) df2 <- data.frame(m = c("m3", "m2", "m1"), dosage = c("a", "b", "c")) library(dplyr) library(tidyr) inner_join(df1, df2, by="m") %>% # join data unite(v,score,dosage, sep = "") %>% # unite two columns in one column spread(m,v) # reshape data # m1 m2 m3 # 1 2c 3b 3a 值都出现在两个数据集中。如果没有,您必须修改过程以匹配您的已排出输出。您是否忽略仅出现在一个数据集中的案例?你保留它们并填充NA吗?还有别的吗?

答案 2 :(得分:1)

使用data.tablet的另一种解决方案: -

df1 <- data.frame(m = c("m1","m2","m3"), score = c(2,3,3))
df2 <- data.frame(m = c("m3", "m2", "m1"), dosage = c("a", "b", "c"))
setDT(df1)
setDT(df2)
setkey(df2, m)
df <- df2[df1]
df[, dos := paste0(score, dosage)]
df <- df[, c("m", "dos")]
df <- t(df)
df

您将获得输出: -

    [,1] [,2] [,3]
m   "m1" "m2" "m3"
dos "2c" "3b" "3a"