user2626657
user2626657

Reputation:

Combine specific values of a dataframe into a dataframe

I have a dataframe, called current, that looks like this:

         V1           V2     V3    V4         V5
1  ILMN_1769720 ILMN_1705907  0 M0039 0.05788020
2  ILMN_1769720 ILMN_1715886  0 M0039 0.07251432
3  ILMN_1769720 ILMN_1652024  0 M0039 0.07069892
4  ILMN_1769720 ILMN_1759792  0 M0039 0.05534454
5  ILMN_1769720 ILMN_1783702  0 M0039 0.07328273
6  ILMN_1769720 ILMN_1763347  0 M0039 0.07546538
7  ILMN_1769720 ILMN_1777261  0 M0039 0.07563834
8  ILMN_1769720 ILMN_1699476  0 M0039 0.06454540
9  ILMN_1769720 ILMN_1813120  0 M0039 0.06648999
10 ILMN_1705907 ILMN_1715886  0 M0039 0.07282304

I want to pick out the 1st of column 1, the 1st value of column 2, the 2nd value of column 2, the 3rd value of column 2, the 4th value of column 2, the 5th value of column 2, the 6th value of column 2, the 7th value of column 2, the 8th value of column 2, and the 9th value of column 2, and combine all of these into a dataframe that looks like this:

         V1          
1  ILMN_1769720 
2  ILMN_1705907
3  ILMN_1715886
4  ILMN_1652024
5  ILMN_1759792
6  ILMN_1783702
7  ILMN_1763347
8  ILMN_1777261
9  ILMN_1699476
10 ILMN_1813120

So far, I have tried as.data.frame(current[1,1],current[1,2],current[2,2],current[3,2],current[4,2],current[5,2],current[6,2],current[7,2],current[8,2],current[9,2]) but it does not seem to work. I have also tried rbind(current[1,1],current[1,2],current[2,2],current[3,2],current[4,2],current[5,2],current[6,2],current[7,2],current[8,2],current[9,2]) but that also does not work. Any suggestions?

Upvotes: 0

Views: 82

Answers (2)

harkmug
harkmug

Reputation: 2785

current <- read.table(header=TRUE,row.names=1,text="V1           V2     V3    V4         V5
1  ILMN_1769720 ILMN_1705907  0 M0039 0.05788020
2  ILMN_1769720 ILMN_1715886  0 M0039 0.07251432
3  ILMN_1769720 ILMN_1652024  0 M0039 0.07069892
4  ILMN_1769720 ILMN_1759792  0 M0039 0.05534454
5  ILMN_1769720 ILMN_1783702  0 M0039 0.07328273
6  ILMN_1769720 ILMN_1763347  0 M0039 0.07546538
7  ILMN_1769720 ILMN_1777261  0 M0039 0.07563834
8  ILMN_1769720 ILMN_1699476  0 M0039 0.06454540
9  ILMN_1769720 ILMN_1813120  0 M0039 0.06648999
10 ILMN_1705907 ILMN_1715886  0 M0039 0.07282304")

data.frame(V1=c(as.character(current[1,1]),as.character(current[1:9,2])))
             V1
1  ILMN_1769720
2  ILMN_1705907
3  ILMN_1715886
4  ILMN_1652024
5  ILMN_1759792
6  ILMN_1783702
7  ILMN_1763347
8  ILMN_1777261
9  ILMN_1699476
10 ILMN_1813120

Upvotes: 1

Tyler Rinker
Tyler Rinker

Reputation: 109874

If your data.frame is named dat you could use mapply as follows:

data.frame(V1 = mapply(function(x, y) dat[x, y], c(1, 1:9), c(1, rep(2, 9))))

##              V1
## 1  ILMN_1769720
## 2  ILMN_1705907
## 3  ILMN_1715886
## 4  ILMN_1652024
## 5  ILMN_1759792
## 6  ILMN_1783702
## 7  ILMN_1763347
## 8  ILMN_1777261
## 9  ILMN_1699476
## 10 ILMN_1813120

Upvotes: 1

Related Questions