由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
DataSciences版 - Ask help for R data restructure, Thanks!!!
相关主题
最近的一些面经L家phone面,悲剧
R问题请教大牛给个大数(+-*)的面试解答吧
急,跪求答案 (moving avg using spark dataframe window functions)上一道我以前喜欢出的题目吧
请问大家有关mixed model (转载)F面经
Memory Error in pandas.concat with Python问一道前几天在版上看见的题
Ask help for R data restructure, Thanks!!! (转载)大整数相乘谁贴个bug free的code
贡献一道面试题目:找出在一个数组里面只出现1次的两个数字今天竟然把MULTIPLY(大数相乘)写完了,发帖庆祝
一道要求常数空间和O(n)时间的排序题这道题怎么解
相关话题的讨论汇总
话题: na话题: data话题: ask话题: comp1
进入DataSciences版参与讨论
1 (共1页)
w*******n
发帖数: 469
1
Given the data like:
ID comp1 Num1 comp2 Num2 comp3 Num3
1 A 2 A 2 D NA
2 B 2 C NA F NA
3 C 2 E NA G NA
4 A 2 D NA H NA
5 B 1 K 1 A 1
6 C 1 B 1 C 1
7 A 2 D 2 B NA
8 B 2 C NA A NA
Want a data like:
ID comp Num
1 A 2
2 B 2
3 C 2
4 A 2
5 B 1
6 C 1
7 A 2
8 B 2
1 A 2
2 C NA
3 E NA
4 D NA
5 K 1
6 B 1
7 D 2
8 C NA
1 D NA
2 F NA
3 G NA
4 H NA
5 A 1
6 C 1
7 B NA
8 A NA
Z**0
发帖数: 1119
2
不就是把数据读入为data frame,然后用rbindlist把数据bind一下?
df <- read.csv('data.txt', sep=' ')
d1 <- df[,c("ID","comp1","Num1")]
d2 <- df[,c("ID","comp1","Num2")]
d3 <- df[,c("ID","comp3","Num3")]
result <- rbindlist(list(d1, d2, d3))
colnames(result) <- c("ID","comp","Num")
如果直接用rbind,需要把每个df的col names统一。
w*******n
发帖数: 469
3
Thanks.
I would like use some function in R like reshape, melt or cast/decast,
supposing I have quite a lot of columns like this.
Or is there any way to do the rbind but ignoring the name difference?

【在 Z**0 的大作中提到】
: 不就是把数据读入为data frame,然后用rbindlist把数据bind一下?
: df <- read.csv('data.txt', sep=' ')
: d1 <- df[,c("ID","comp1","Num1")]
: d2 <- df[,c("ID","comp1","Num2")]
: d3 <- df[,c("ID","comp3","Num3")]
: result <- rbindlist(list(d1, d2, d3))
: colnames(result) <- c("ID","comp","Num")
: 如果直接用rbind,需要把每个df的col names统一。

Z**0
发帖数: 1119
4
Here you go.
df <- read.csv('data.txt', sep=' ')
l <- reshape(df, idvar="ID", varying = list(c("comp1","comp2","comp3"),c("
Num1","
Num2","Num3")), v.names = c("comp", "Num"), direction="long")

【在 w*******n 的大作中提到】
: Thanks.
: I would like use some function in R like reshape, melt or cast/decast,
: supposing I have quite a lot of columns like this.
: Or is there any way to do the rbind but ignoring the name difference?

n*****3
发帖数: 1584
5
you can use melt for the reshape package.

【在 Z**0 的大作中提到】
: Here you go.
: df <- read.csv('data.txt', sep=' ')
: l <- reshape(df, idvar="ID", varying = list(c("comp1","comp2","comp3"),c("
: Num1","
: Num2","Num3")), v.names = c("comp", "Num"), direction="long")

c***3
发帖数: 280
6
Zer0 解法的简化版(用column number代替column name):
l <- reshape(df, direction="long", idvar=1,
varying=c(c(2,4,6), c(3,5,7)), sep="")
1 (共1页)
进入DataSciences版参与讨论
相关主题
这道题怎么解Memory Error in pandas.concat with Python
如何求一个整数阶乘的各位数字和Ask help for R data restructure, Thanks!!! (转载)
来发个我的Leetcode的Python答案吧贡献一道面试题目:找出在一个数组里面只出现1次的两个数字
发一个Startup的面经 - Affirm一道要求常数空间和O(n)时间的排序题
最近的一些面经L家phone面,悲剧
R问题请教大牛给个大数(+-*)的面试解答吧
急,跪求答案 (moving avg using spark dataframe window functions)上一道我以前喜欢出的题目吧
请问大家有关mixed model (转载)F面经
相关话题的讨论汇总
话题: na话题: data话题: ask话题: comp1