找回密码
 注册
查看: 2055|回复: 1

求助,R语言编程问题

[复制链接]
发表于 2014-11-1 23:57:46 | 显示全部楼层 |阅读模式
本人是在美国一个大学求学的学生,是刚接触R语言的菜鸟,我有如下关于R语言的问题需要牛人的帮助,如能如愿,感谢不尽。 相关数据.zip (737 Bytes, 下载次数: 4)
如能回复解决方案给本人的email则更为感激:zyuan@email.unc.edu
There are 4 attached files  as follows:
   f1.csv – person file 1,
                 including   id, item by position (not by ID)
   f2.csv – person file 2,
                 including   pid (id, but different from id in file 1), item by position (not by ID)
             these two may/maynot overlap
   f1id.csv – id to name crosswalk,
                     including firstname, lastname, id
   f2id.csv – pid to name crosswalk,
                     including firstname, lastname, pid

What need to do is:
1.       Merge names onto f1.csv and f2.csv using files f1id.csv and f2id.csv

2.       Merge f1.csv and f2.csv based on firstname and lastname

Also the merge need to follow the instruction:
                       1.Organize columns after the merge
   2.Names and IDs first, then items in name order
   3.The merge need to do in two ways:
                 1) cannot lose anyone f1.csv and f2.csv
                                         2)Keep only people in both f1.csv and f2.csv
回复

使用道具 举报

发表于 2016-2-22 16:26:47 | 显示全部楼层
> setwd("G:/RBook/")
> f1 <- read.table(file = "f1.txt", header = TRUE)
> f1id <- read.table(file = "f1id.txt", header = TRUE)
> f2 <- read.table(file = "f2.txt", header = TRUE)
> f2id <- read.table(file = "f2id.txt", header = TRUE)
>
>
> new <- merge(f1, f1id, by = "id", all = TRUE)
>
> new
   id i001 i002 i003 fname lname
1 123    3    2    4  John     W
2 258    4    3    5  Jada     W
3 321    4    5    3  Tana     W
>
> id <- new$id
> fname <- new$fname
> lname <- new$lname
> i001 <- new$i001
> i002 <- new$i002
> i003 <- new$i003
>  
>
> new2 <- data.frame(fname = fname, lname = lname, id = id,
+ i001 = i001, i002 = i002, i003 = i003)
>
> new2
  fname lname  id i001 i002 i003
1  John     W 123    3    2    4
2  Jada     W 258    4    3    5
3  Tana     W 321    4    5    3
>
> newid <- merge(f1id, f2id, by = c("fname", "lname"))
>
> newid
  fname lname  id pid
1  John     W 123 989
>
>



不知道行不行。
我也是初学者,互相交流学习一下。
回复 支持 1 反对 0

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|生物统计家园 网站价格

GMT+8, 2024-11-21 22:18 , Processed in 0.047718 second(s), 20 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表