Adding a column to a dataframe in R
That is a pretty standard use case for apply()
:
R> vec <- 1:10
R> DF <- data.frame(start=c(1,3,5,7), end=c(2,6,7,9))
R> DF$newcol <- apply(DF,1,function(row) mean(vec[ row[1] : row[2] ] ))
R> DF
start end newcol
1 1 2 1.5
2 3 6 4.5
3 5 7 6.0
4 7 9 8.0
R>
You can also use plyr
if you prefer but here is no real need to go beyond functions from base R.
How to add a new column in dataframe based on a single character in dataframe names?
Considering name restriction in R, I remove brackets from dataframe name, so I think the reproducible example should be something like this :
df_list <- list()
Height <- c(175,180,179)
Weight <- c(75,85,79)
Person <- c('Alex','Gerard','Clyde')
df_list$'1.3.A' <- data.frame(Person,Height,Weight)
Person <- c('Missy','Britany','Sussie')
df_list$'2.2.A' <- data.frame(Person,Height,Weight)
Person <- c('Luke','Alex','Haley')
df_list$'1.1.B' <- data.frame(Person,Height,Weight)
$`1.3.A`
Person Height Weight
1 Alex 175 75
2 Gerard 180 85
3 Clyde 179 79
$`2.2.A`
Person Height Weight
1 Missy 175 75
2 Britany 180 85
3 Sussie 179 79
$`1.1.B`
Person Height Weight
1 Luke 175 75
2 Alex 180 85
3 Haley 179 79
Without using any libraries, similar to Anoushiravan R, I use strsplit and Map to extract Trial, Day and Group value from dataframe name :
ColAdd <- \(DF,Names){
DF[,c('Trial','Day','Group')] <-
(Names |> strsplit(split = '.', fixed = T))[[1]] |>
rep(nrow(DF)) |>
matrix(ncol=3,byrow = T)
return(DF)
}
df_list <- Map(ColAdd,df_list,names(df_list))
$`1.3.A`
Person Height Weight Trial Day Group
1 Alex 175 75 1 3 A
2 Gerard 180 85 1 3 A
3 Clyde 179 79 1 3 A
$`2.2.A`
Person Height Weight Trial Day Group
1 Missy 175 75 2 2 A
2 Britany 180 85 2 2 A
3 Sussie 179 79 2 2 A
$`1.1.B`
Person Height Weight Trial Day Group
1 Luke 175 75 1 1 B
2 Alex 180 85 1 1 B
3 Haley 179 79 1 1 B
How do I add a column to a data frame consisting of minimum values from other columns?
You can use apply()
function to do this. See below.
df$C <- apply(df, 1, min)
The second argument allows you to choose the dimension in which you want min
to be applied, in this case 1, applies min to all columns in each row separately.
You can choose specific columns from the dataframe, as follows:
df$newCol <- apply(df[c('A','B')], 1, min)
How do add a column in a data frame in R
You can use a map. (UPDATED to use stringsAsFactors = FALSE
)
df <- data.frame( Name = c('A', 'C', 'D', 'E', 'H', 'Z', 'M'),
Count = c(100,10,40,30,3,20,50), stringsAsFactors = FALSE)
Categories <- list(Cat1 = c('A','D'),
Cat2 = c('C','Z'),
Cat3 = c('E','H'),
Cat10 = 'M')
nams <- names( Categories )
nums <- sapply(Categories, length)
CatMap <- unlist( Map( rep, nams, nums ) )
names(CatMap) <- unlist( Categories )
df <- transform( df, Category = CatMap[ Name ])
R - Add a column of data to an existing column in a dataframe
You will need to make sure that the column names are same when appending data.
In your case:
df2 <- data.frame(x2, y2) #creating a dataframe
names(df2) <- names(df) #changing the column header names as this is a requirement for append
df <- rbind(df, df2) #appending
R: How do I add a column to a dataframe based on paired values in another dataframe with different length?
Using left_join from dplyr package:
library(dplyr)
data <- left_join(df1,df2, by="id")
Related Topics
How to Summarizing Data Statistics Using R
Add Missing Value in Column with Value from Row Above
What Does Passing an Ellipsis (...) as an Argument Mean in R
Ddply Multiple Quantiles by Group
Different Results with Randomforest() and Caret's Randomforest (Method = "Rf")
R - Waiting for Page to Load in Rselenium with Phantomjs
Count Number of Vector Values in Range with R
How to Change the Order of the Panels in Simple Lattice Graphs
Dealing with Readlines() Function in R
Datatable Is Not Printed in Combination with Cat Command in Rmd/Rstudio
How to Build Multiclass Svm in R
Add Na Value to Ggplot Legend for Continuous Data Map
Importing Wikipedia Tables in R
What Are Helpful Optimizations in R for Big Data Sets
How to Change Node and Link Colors in R Googlevis Sankey Chart