My column titles are not correct. I want to rename all my column of a matrix (because i have v1,v2,v3,..) according to data frame (the name of the first column corresponds to the first title of my data frame). I have to repeat this for my 39 columns. So the goal would be to do a for-loop.
df1 is the matrix that has to be changed.
for (i in 1:39) {
names(df1[,i]) <- names(dfnorm[,i])
}
This code is not working.
If I understand correctly, this should work:
names(df1)[1:39] <- names(dfnorm)[1:39]
Related
I have a dataframe with over 100 columns. Post implementation of certain conditions, I need a subset of the dataframe with the columns that are listed in a separate array.
The array has 50 entries with 2 columns. The first column has the selected variable names and the second column has some associated values.
I wish to build a new data frame with just the variables mentioned in the the first column of the separate array. Could you please point me as to how to proceed?
Try this:
library(dplyr)
iris <- iris %>% select(contains(dataframe_with_names$names))
In R you can use square brackets [rows, columns] to select specific rows or specific columns. (Leaving either blank selects all).
If you had a vector of column names you wanted to keep called important_columns you could select only those columns with:
myData[,important_columns]
In your case the vector of column names is actually a column in your array. So you select that column and use it as your vector:
myData[, array$names]
I am working on data set for analysis.
I have to remove column name of single variable of data frame in r.
I have used colnames(df)<-NULL function,it removes all colnames of dataframe
The excepted output is:
I don't really get the expected output, but maybe this is what you're looking for
colnames(df)[i] <- NA
'i' is the column location (for example 1,2,3 etc)
I have two dataframes
dataframe 1 has around million rows.. and its has two columns named 'row' and 'columns' that has the index of row and column of another dataframe (i.e. dataframe 2)..
i want to extract the values from dataframe 2 with the indexes stated in the columns named 'row' and 'columns' for each row in dataframe1.
I used a simple for loop to get the solution but it is time consuming and takes around 9 minutes, is there any other way with functions in R to solve this problem?
for(i in 1:nrow(datafram1)) {
dataframe1$value[i] = dataframe2[dataframe1$row[i],dataframe1$columns[i]]
}
You actually don't need a for loop to do this. Just add the new column to the Data Frame using the row and column names:
DataFrame1$value <- DataFrame2[DataFrame1$row, DataFrame1$column]
This should work a lot faster. If you wanted to try it a different way you could try adding the values to a new vector and then using cbind to join the vector to the Data Frame. The fact that you're trying to update the whole Data Frame during the loop is most likely what's slowing it down.
Maybe you can try the code below
dataframe1$value <- dataframe2[as.matrix(dataframe1[c("row","columns")])]
Sionce your loop only consider the rows in df1, you can cut the surplus roes on df2 and then use cbind:
dataframe2 <- dataframe2[nrow(dataframe1),]
df3 <- cbind(dataframe1, dataframe2)
I'm a newbie to R. I'm currently working with two dataframes, one containing initial values, and another containing values that have been computed using the original data.
My new dataframe for the computed values is built like this:
reldf <- data.frame(matrix(ncol = 13, nrow = nrow(glasgow2001)))
names <- c("2001r","2002r","2003r","2004r","2005r","2006r","2007r","2008r",
"2009r","2010r","2011r","2012r","2013r")
However, in order to remerge the computed values with the original dataframe, I want to be able to extract the original row names from the first data frame and apply them to this one. And this is where I'm completely lost.
Basically, how do I extract row names in R and apply them onto a new dataframe?
You can assign column names with names()
row.names(reldf) <- names
I have 68 data files- all with the same identifiers-but with different indicators. I converted these individual files into a list with each data frame as a separate element.
The first row of every data frame is a year, which I would like to paste to the column name. I want to be able to separate it by "_".
For example, right now the column name is Arbeitslose, and the row under it has 2018. I would like the column name to become Arbeitslose_2018.
I know how to do this on a single data frame. The code I used is below.
RAW_2[1,] <- as.character(RAW_2[1,]) # Converting the fist row to a character.
colnames(RAW_2) <- paste(colnames(RAW_2),RAW_2[1, ], sep = "_") # Paste Year (Row 2) and columnname
RAW_2 <- RAW_2[rownames(RAW_2) != 1, ] # Drop 1st row which is the years - now abundant
but I dont know how to do this for a list.
I cannot merge the data frames into a single one, because the column names are not unique. I would need to do this step for me to be able to merge it into a data set and proceed. I'm forced to work with lists, something I am horrible with.
Is there an easy way to do this? I am quite lost on how to proceed.
You can use lapply()
rename_col <- function(x){
colnames(x) <- paste0(colnames(x),x[1,],sep="_")
x[-1,]
}
#df_list as your list of data.frames
lapply(df_list,rename_col)