Reputation: 23
I am trying to construct a code chunk that, after inputting data from an Excel sheet, constructs a data frame of "refereed publications" from within the past five years (the initial data are filtered through a couple of subset commands on lines 1 and 2). I've pasted some of the code below that I think helps explain what I'm trying to do and how I am trying to do it.
The third line pubsfive <- within(pubsfive, rwnum <- 1)
is to initialize a new column within the pubsfive
data frame. Lastly, I'm trying to run a for
loop in an attempt to number each line by its row name (1 through 10, in this case). I don't exactly know what I'm doing with the for
loop, but I'm trying to code for each row in the new column called, rwnum
that simply outputs the row number. I should be able to concatenate all of the columns and then pass the object to kable
to then print a nice 5-year publications list.
This probably isn't the most eloquent code, so I am open to any clarifying questions or tips.
pubsfive <- subset(pubs, pubs$Contribution == "Refereed Publications")
pubsfive<- subset(pubsfive, pubsfive$year >= 2014, select = c(Authors, year, Title))
pubsfive <- within(pubsfive, rwnum <- 1)
pubsfive <- for (i in 1:nrow(pubsfive))
{within(pubsfive, rwnum <- rownames(pubsfive)[i])}
Upvotes: 2
Views: 59
Reputation: 226162
I'm not sure what's wrong with this:
pubsfive <- subset(pubs,
Contribution == "Refereed Publications" & year > 2014,
select = c(Authors, year, Title))
pubsfive$rwnum <- rownames(pubsfive)
The first line is a slightly more compact way of doing your first two lines, the third assigns a vector to the new column rwnum
... if you want the row numbers first you might prefer
pubsfive <- data.frame(rwname=rownames(pubsfive),
pubsfive)
It's not clear to me whether you want the row names or seq(nrow(pubsfive))
...
Upvotes: 1