I have a big table in R, now I want to select the odd rows and paste a label before the first element of this row

好久不见. 提交于 2019-12-06 10:03:17

问题


A=matrix(0,4,2)

A[1,1]=2
A[1,2]=3
A[2,1]=2
A[2,2]=3
A[3,1]=2
A[3,2]=3
A[4,1]=2
A[4,2]=3

Now I want to pick up row 2,4 and return this is odd before the first element of the row.

But I don't know how to make a loop to pick up row 2,4


回答1:


If I understand your question correctly, you want to display some text and the first element of all odd rows. You can try this:

cat(paste("This is odd", A[c(2,4),1], "\n"))

No need for a loop there. Should you want to work with a larger matrix, and take all odd rows, you can use seq(2, nrow(A), by=2) instead of c(2,4).



来源:https://stackoverflow.com/questions/6183394/i-have-a-big-table-in-r-now-i-want-to-select-the-odd-rows-and-paste-a-label-bef

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!