r

dividing columns in a list in R

霸气de小男生 提交于 2021-02-10 22:20:28
问题 I am trying to add a column in a data.frame and have the output of that new column be the division of column 3 by column 2, here is an example: mylist <- list( a=c(0,1,2),b=c(0,2,3),c=c(0,4,5)) this returns: $a [1] 0 1 2 $b [1] 0 2 3 $c [1] 0 4 5 I would like to return: $a [1] 0 1 2 2 $b [1] 0 2 3 1.5 $c [1] 0 4 5 1.25 Please help 回答1: We can loop through the list with lapply , divide the 3rd element by the 2nd and concatenate with the original vector lapply(mylist, function(x) c(x, x[3]/x[2]

dividing columns in a list in R

心不动则不痛 提交于 2021-02-10 22:18:07
问题 I am trying to add a column in a data.frame and have the output of that new column be the division of column 3 by column 2, here is an example: mylist <- list( a=c(0,1,2),b=c(0,2,3),c=c(0,4,5)) this returns: $a [1] 0 1 2 $b [1] 0 2 3 $c [1] 0 4 5 I would like to return: $a [1] 0 1 2 2 $b [1] 0 2 3 1.5 $c [1] 0 4 5 1.25 Please help 回答1: We can loop through the list with lapply , divide the 3rd element by the 2nd and concatenate with the original vector lapply(mylist, function(x) c(x, x[3]/x[2]

R: How to “unnest” a nested list into data.frame?

a 夏天 提交于 2021-02-10 22:18:04
问题 I have l1 = list(SeriousDlqin2yrs = list(prediction = "0", prediction_probs = list(`0` = 0.5, `1` = 0.5))) l2 = list(SeriousDlqin2yrs = list(prediction = "1", prediction_probs = list(`0` = 0.6, `1` = 0.4))) l12 = list(l1, l2) data.frame.output = l12 %>% purrr::reduce(dplyr::bind_rows) %>% unnest(cols = c("SeriousDlqin2yrs", "0", "1")) and I am expect a dataframe like this (expressed in CSV format) SeriousDlqin2yrs$prediction, SeriousDlqin2yrs$prediction$0, SeriousDlqin2yrs$prediction$1 0, 0.5

R: How to “unnest” a nested list into data.frame?

家住魔仙堡 提交于 2021-02-10 22:16:57
问题 I have l1 = list(SeriousDlqin2yrs = list(prediction = "0", prediction_probs = list(`0` = 0.5, `1` = 0.5))) l2 = list(SeriousDlqin2yrs = list(prediction = "1", prediction_probs = list(`0` = 0.6, `1` = 0.4))) l12 = list(l1, l2) data.frame.output = l12 %>% purrr::reduce(dplyr::bind_rows) %>% unnest(cols = c("SeriousDlqin2yrs", "0", "1")) and I am expect a dataframe like this (expressed in CSV format) SeriousDlqin2yrs$prediction, SeriousDlqin2yrs$prediction$0, SeriousDlqin2yrs$prediction$1 0, 0.5

How to reshape a wider data.frame to longer data.frame in R? [duplicate]

半世苍凉 提交于 2021-02-10 22:15:51
问题 This question already has answers here : Transpose and Merge columns in R [duplicate] (3 answers) Reshaping data.frame from wide to long format (9 answers) Closed 7 months ago . I was playing with pivot_longer and pivot_wider but probably am missing something. I have a data.frame like D_Wider and would like to convert it to something like D_longer . any way forward? library(tidyverse) D_Wider <- data.frame(A = 15, S = 10, D = 25, Z = 16) Desired Output D_Longer <- data.frame(Stations = c("A",

dividing columns in a list in R

前提是你 提交于 2021-02-10 22:11:00
问题 I am trying to add a column in a data.frame and have the output of that new column be the division of column 3 by column 2, here is an example: mylist <- list( a=c(0,1,2),b=c(0,2,3),c=c(0,4,5)) this returns: $a [1] 0 1 2 $b [1] 0 2 3 $c [1] 0 4 5 I would like to return: $a [1] 0 1 2 2 $b [1] 0 2 3 1.5 $c [1] 0 4 5 1.25 Please help 回答1: We can loop through the list with lapply , divide the 3rd element by the 2nd and concatenate with the original vector lapply(mylist, function(x) c(x, x[3]/x[2]

dividing columns in a list in R

蓝咒 提交于 2021-02-10 22:10:57
问题 I am trying to add a column in a data.frame and have the output of that new column be the division of column 3 by column 2, here is an example: mylist <- list( a=c(0,1,2),b=c(0,2,3),c=c(0,4,5)) this returns: $a [1] 0 1 2 $b [1] 0 2 3 $c [1] 0 4 5 I would like to return: $a [1] 0 1 2 2 $b [1] 0 2 3 1.5 $c [1] 0 4 5 1.25 Please help 回答1: We can loop through the list with lapply , divide the 3rd element by the 2nd and concatenate with the original vector lapply(mylist, function(x) c(x, x[3]/x[2]

Sort each row of character strings alphabetically in R

心不动则不痛 提交于 2021-02-10 21:36:07
问题 I've looked around and can't seem to find a decent way to solve this issue. I have a column that has rows of names. I'd like to sort each row alphabetically so that I can later identify rows that have the same names just in different orders. The data looks like this: names <- c("John D., Josh C., Karl H.", "John D., Bob S., Tim H.", "Amy A., Art U., Wes T.", "Josh C., John D., Karl H.") var1 <- rnorm(n = length(names), mean = 0, sd = 2) var2 <- rnorm(n = length(names), mean = 20, sd = 5) df <

How to combine the across () function with mutate () and case_when () to mutate values in multiple columns according to a condition?

て烟熏妆下的殇ゞ 提交于 2021-02-10 20:51:26
问题 I have demographic data set, which includes the age of people in a household. This is collected via a survey and participants are allowed to refuse providing their age. The result is a data set with one household per row (each with a household ID code), and various household characteristics such as age in the columns. Refused responses as coded as "R", and you could re-create a sample using the code below: df <- list(Household_ID = c("1A", "1B", "1C", "1D", "1E"), AGE1 = c("25", "47", "39",

How to combine the across () function with mutate () and case_when () to mutate values in multiple columns according to a condition?

荒凉一梦 提交于 2021-02-10 20:48:37
问题 I have demographic data set, which includes the age of people in a household. This is collected via a survey and participants are allowed to refuse providing their age. The result is a data set with one household per row (each with a household ID code), and various household characteristics such as age in the columns. Refused responses as coded as "R", and you could re-create a sample using the code below: df <- list(Household_ID = c("1A", "1B", "1C", "1D", "1E"), AGE1 = c("25", "47", "39",