lapply

R substr function on multiple columns

こ雲淡風輕ζ 提交于 2021-02-05 11:57:21
问题 I have 3 columns. First column has unique ID, second and third columns have string data and some NA data. I need to extract info from column 2 and put it in separate columns and do the same thing for column 3. I am building a function as follows, using for loops. I need to split the columns after the third letter. [For example in the V1 column below, I need to break AAAbbb as AAA and bbb and put them in separate columns. I know I can use substr to do this. I am new to R, please help. UID * V1

Use R to write multiple sheets in excel with dynamic sheetNames

前提是你 提交于 2021-02-05 11:25:08
问题 This can easily be done using for loop but I am looking for a solution with lapply or dplyr . I have multiple dataframes which I want to export to excel file in separate sheets (I saw many questions and answers on similar lines but couldn't find one that addressed naming sheets dynamically). I want to name the sheet by the name of the dataframe. For simplicity, I have named the dataframes in a pattern (say df1 to df10). How do I do this? Below is a reproducable example with my attempt with

converting columns to factor over list of dataframes

你说的曾经没有我的故事 提交于 2021-02-05 11:15:33
问题 I'm trying to convert several columns in a list of dataframes into factors. I've tried this, but it doesn't seem to convert the columns into factors: factor_cols_REx <- c('GESLACHT','GEVKL','BEROEP') for (i in (1:9)) { dataset_RE10_2014[[i]] <- lapply(dataset_RE10_2014[[i]][factor_cols_REx],factor) dataset_RE10_2015[[i]] <- lapply(dataset_RE10_2015[[i]][factor_cols_REx],factor) } Any ideas on how to fix this? 回答1: Let me know if I understood correctly #DATA dat = list(A = mtcars, B = mtcars)

Using lapply to create new columns based on old columns

倾然丶 夕夏残阳落幕 提交于 2021-02-05 09:34:31
问题 My data looks as follows: DF <- structure(list(No_Adjusted_Gross_Income = c(183454, 241199, 249506 ), NoR_from_1_to_5000 = c(1035373, 4272260, 1124098), NoR_from_5000_to_10000 = c(319540, 4826042, 1959866)), row.names = c(NA, -3L), class = c("data.table", "data.frame")) val <- c(2500.5, 7500) vn <- c("AGI_from_1_to_5000", "AGI_from_5000_to_10000") No_Adjusted_Gross_Income NoR_from_1_to_5000 NoR_from_5000_to_10000 1: 183454 1035373 319540 2: 241199 4272260 4826042 3: 249506 1124098 1959866 I

R import multiple csv files

拈花ヽ惹草 提交于 2021-02-05 08:11:37
问题 I want to import multiple TSV Files (yes: T SV) in R. Reading a single file with an selection of spacific columns works well by using: data00<-read.csv(file = '/Volumes/2018/06_abteilungen/bi/analytics/tools/adobe/adobe_analytics/adobe_analytics_api_rohdaten/api_via_data_feed_auf_ftp/beispiel_datenexporte_data_feed/01sssamsung4de_20180501-000000.tsv', sep ="\t", fill = TRUE, quote='', header = FALSE )[ ,c(287, 288, 289, 290, 291, 292, 293, 304, 370, 661, 662, 812, 813, 994, 995, 1002)] Now i

R: converting fractions into decimals in a data frame

萝らか妹 提交于 2021-02-05 07:14:45
问题 I am trying to convert a data frame of numbers stored as characters in a fraction form to be stored as numbers in decimal form. (There are also some integers, also stored as char.) I want to keep the current structure of the data frame, i.e. I do not want a list as a result. Example data frame (note: the real data frame has all elements as character, here it is a factor but I couldn't figure out how to replicate a data frame with characters): a <- c("1","1/2","2") b <- c("5/2","3","7/2") c <-

How to reproduce all column names when producing a table to cross reference column names and datatypes from multiple dbfs in R

这一生的挚爱 提交于 2021-01-29 19:39:12
问题 This is a follow up question to Implementing lists in a for loop in R to produce a table of column names and datatypes from multiple dbfs. I’m trying to extract the column names and associated datatypes from a number of dbfs and put the results into a table to cross reference which column names and datatypes appear in which dbfs. The dbfs have different numbers of columns so I’ve used rbind and lapply to fill missing values with NULL in the resulting table. Although the script I have works to

proportion of factors and dummies

▼魔方 西西 提交于 2021-01-29 11:32:05
问题 I have a data set full of factors and dummies, I want to see the proportion of each value after dplyr::group_by(cyl) mtcars; rownames(mtcars) <- NULL df <- mtcars[,c(2,8,9)] head(df) cyl vs am 1 6 0 1 2 6 0 1 3 4 1 1 4 6 1 0 5 8 0 0 6 6 1 0 Expected answer I have in cyl 6 6 6 6 for vs column two of them is 1 two of them 0 1 0 6 50% 50% 4 100% 0% 8 0% 100% same as this for column am too 回答1: Here's a first crack: (df %>% pivot_longer(-cyl) ## spread out variables (vs, am) %>% group_by(cyl,name

Using Apply or Vectorize to apply custom function to a dataframe

左心房为你撑大大i 提交于 2021-01-29 09:14:26
问题 I am attempting to apply a custom function that calls components of that dataframe to do a calculation. I have made a trivial example below because my actual problem is very hard to make a reproducible example. In the below example I want to have the first two columns be added together to create a third column which is the sum of them. Below is an example I found online that gets close to what I want: celebrities=data.frame(name=c("Andrew","matt","Dany","Philip","John","bing","Monica"), age=c

write list of dataframes to multiple excel files

筅森魡賤 提交于 2021-01-28 22:13:11
问题 I have a list of dataframes. Conveniently named: list.df and the objects, which are dataframes, are just this: list.df[[1]] list.df[[2]] list.df[[3]] I am trying to use lapply to write each of the list.df objects to a seperate excel sheet. I can't use the xlsx library because my workplace disables everything Java... so I've been trying write_xlsx. I've tried the following: lapply(names(list.df), function (x) write_xlsx(list.df[[x]], file=paste(x, "xlsx", sep="."))) But nothing happens. Any