Changing a List to a Dataframe in R - r

I have used the "htmltab" library to get data on the NFL draft and combine. The data has been selected fine but they are lists at the moment. I intend to merge them and perform analysis the data. at the moment it looks like this:
image List of combine 2016 1
Whenever I try use the unlist method I lose the headers of the columns and they are still remaining as a list.
any suggestions on this?
urlcom16 <- "http://nflcombineresults.com/nflcombinedata.php?
year=2016&pos=&college="
com16 <- htmltab(doc=urlcom16, which=1)

Try as.data.frame(com16). If it doesn't work, you might not have the same vector length in each list entry.

Related

R - how to use a function on a list of data frames

I have a little problem with my code. I hope you can help me :)
I used a function apply to create a list of 20 data frames (data about stock index returns, grouped by year and index - about three companies and the stock, for 5 years). And now I want to use function with two arguments (it calculates proportion of covariance of the returns for selected company and the stock to variance (for every year) - this is why I'm trying to group the data. How to do it... automatically, without manual typing code for every year and company?
I don't have any idea if I should use for loop or there is any other way...?
And the other thing is in which way can I delete uneccesary columns from list of data frames?
I'll be thankful for your help.
And sorry for my English :D
You may consider purrr::map_dfr(). The first argument will be your list of data frames, and the second the action to do with that data frame. The final result will be a single data frame uniting the result of all of the above. Your code will likely look something like this:
purrr::map_dfr(list_of_dataframes, function(x) {...})
Within the bracketes, instead of ... insert your logic. In that context, x will be the same as list_of_dataframes[[1]], and then list_of_dataframes[[2]], etc.
You may want to consult the documentation of the package purrr for further details.

How to merge a set of lists into a single data frame

I am new to R and coding in general, so please bear with me.
I have a spreadsheet that has 7 sheets, 6 of these sheets are formatted in the same way and I am skipping the one that is not formatted the same way.
The code I have is thus:
lst <- lapply(2:7,
function(i) read_excel("CONFIDENTIAL Ratio 062018.xlsx", sheet = i)
)
This code was taken from this post: How to import multiple xlsx sheets in R
So far so good, the formula works and I have a large list with 6 sub lists that appears to represent all of my data.
It is at this point that I get stuck, being so new I do not understand lists yet, and really need the lists to be merged into one single data frame that looks and feels like the source data (so columns and rows).
I cannot work out how to get from a list to a single data frame, I've tried using R Bind and other suggestions from here, but all seem to either fail or only partially work and I end up with a data frame that looks like a list etc.
If each sheets has the same number of columns (ncol) and same names (colnames) then this will work. It needs the dplyr pacakge.
require(dplyr)
my_dataframe <- bind_rows(my_list)

How can I extract a single element from a list into a data frame?

I compiled a list of ~60 data frames to keep my RStudio environment tidy.
I will need to occasionally extract a single element into a data frame so that I can work on it before putting it back into the list - how can this extract be achieved?
I am aware that I can manipulate the list element directly, but that isn't ideal and being able to extract the data frame would serve me better for my needs.
If dflist is your list of dataframes, then the easiest way to work on element n would be something like
df <- dflist[[n]]
#...work on df...then
dflist[[n]] <- df

copying data from one data frame to other using variable in R

I am trying to transfer data from one data frame to other. I want to copy all 8 columns from a huge data frame to a smaller one and name the columns n1, n2, etc..
first I am trying to find the column number from which I need to copy by using this
x=as.numeric(which(colnames(old_df)=='N1_data'))
Then I am pasting it in new data frame this way
new_df[paste('N',1:8,'new',sep='')]=old_df[x:x+7]
However, when I run this, all the new 8 columns have exactly same data. However, instead if I directly use the value of x, then I get what I want like
new_df[paste('N',1:8,'new',sep='')]=old_df[10:17]
So my questions are
Why I am not able to use the variable x. I added as.numeric just to make sure it is a number not a list. However, that does not seem to help.
Is there any better or more efficient way to achieve this?
If I'm understanding your question correctly, you may be overthinking the problem.
library(dplyr);
new_df <- select(old_df, N1_data, N2_data, N3_data, N4_data,
N5_data, N6_data, N7_data, N8_data);
colnames(new_df) <- sub("N(\\d)_data", "n\\\\1", colnames(new_df));

r create and address variable in for loop

I have multiple csv-files in one folder. I want to load each csv-file in this folder into one separate data frame. Next, I want to extract certain elements from this data frame into a matrix and calculate the mean of all these matrixes.
setwd("D:\\data")
group_1<-list.files()
a<-length(group_1)
mferg_mean<-data.frame
for(i in 1:a)
{
assign(paste0("mferg_",i),read.csv(group_1[i],header=FALSE,sep=";",quote="",dec=",",col.names=1:90))
}
As there are 11 csv-files in the folder I now have the data frames
mferg_1
to
mferg_11
How can I address each data frame in this loop? As mentioned, I want to extract certain elements from each data frame to a matrix. I would imagine it something like this:
assign(paste0("mferg_matrix_",i),mferg_i[1:5,1:10])
But this obviously does not work because R does not recognize mferg_i in the loop. How can I address this data frame?
This is not something you should probably be using assign for in the first place. Working with a bunch of different data.frames in R is a mess, but working with a list of data.frames is much easier. Try reading your data with
group_1<-list.files()
mferg <- lapply(group_1, function(filename) {
read.csv(filename,header=FALSE,sep=";",quote="",dec=",",col.names=1:90))
})
and you get each each value with mferg[[1]], mferg[[1]], etc. And then you can create a list of extractions with
mferg_matrix <- lapply(mferg, function(x) x[1:5, 1:10])
This is the more R-like way to do things.
But technically you can use get to retrieve values like you use assign to create them. For example
assign(paste0("mferg_matrix_",i),get(paste0("mferg_",i))[1:5,1:10])
but again, this is probably not a smart strategy in the long run.

Resources