簡體   English   中英

根據R中行的值向數據框中添加不同數量的列

[英]Add a different number of columns to dataframe depending on value from row in R

您能幫我為數據框的行添加不同數量的列嗎?

例如,我有此DataFrame:

employee <- c('John','Peter','Jolie', 'Katy', 'Pauline')
numberofmonths <- c(7, 5, 11, 3, 12)
employers <- data.frame(employee,numberofmonths)

  employee numberofmonths
1     John              7
2    Peter              5
3    Jolie             11
4     Katy              3
5  Pauline             12

現在,我想向employers添加“雇員”名稱所包含的字符列。

所以我想要這樣的事情:

  employee numberofmonths  i  i  i  i  i  i  i
1     John              7 A1 A2 A3 A4 NA NA NA
2    Peter              5 A1 A2 A3 A4 A5 NA NA
3    Jolie             11 A1 A2 A3 A4 A5 NA NA
4     Katy              3 A1 A2 A3 A4 NA NA NA
5  Pauline             12 A1 A2 A3 A4 A5 A6 A7

我已經嘗試過以下腳本:

for (i in (1:nrow(employers))) {
  for (j in nchar(as.vector(employers[i,]$employee))){

  employers<-cbind(employers, i=paste("A", i, sep=""))
}}

但是,與其為約翰和...提供A1:A4,還不為Pauline提供A1:A7,而是為所有人提供A1:A5:

  employee numberofmonths  i  i  i  i  i
1     John              7 A1 A2 A3 A4 A5
2    Peter              5 A1 A2 A3 A4 A5
3    Jolie             11 A1 A2 A3 A4 A5
4     Katy              3 A1 A2 A3 A4 A5
5  Pauline             12 A1 A2 A3 A4 A5

當然,對於名稱字符比其他字符少的名稱,我們將使用NA列。 我正在處理帶有許多字符串的大數據框,因此任何手動操作都可以使用。 這僅是示例,因此Ai值沒有任何意義。

這是使用plyr的解決方案:

require(plyr)
cbind(employers, rbind.fill.matrix(lapply(nchar(employee),  function(z) t(paste0("A", 1:z)))))

嘗試這個:

nc<-nchar(as.character(employers$employee))
mat<-matrix(NA_character_,nrow=nrow(employers),ncol=max(nc))
indices<-sequence(nc)
values<-paste0("A",indices)
mat[cbind(rep(1:nrow(employers),nc),indices)]<-values
cbind(employers,mat) 
#  employee numberofmonths  1  2  3  4    5    6    7
#1     John              7 A1 A2 A3 A4 <NA> <NA> <NA>
#2    Peter              5 A1 A2 A3 A4   A5 <NA> <NA>
#3    Jolie             11 A1 A2 A3 A4   A5 <NA> <NA>
#4     Katy              3 A1 A2 A3 A4 <NA> <NA> <NA>
#5  Pauline             12 A1 A2 A3 A4   A5   A6   A7
employee <- c('John','Peter','Jolie', 'Katy', 'Pauline')
numberofmonths <- c(7, 5, 11, 3, 12)
employers <- data.frame(employee,numberofmonths)
employers$employee <- as.character(employers$employee)

emp_app <- as.data.frame(matrix(NA,
                                nrow = nrow(employers), 
                                ncol = max(nchar(employers$employee))))
for (i in seq_len(nrow(employers))) {
  nm_lngth <- nchar(employers$employee)[i]
  nm_string <- paste0("A", seq_len(nm_lngth))
  for (j in seq_len(nm_lngth)) {
    emp_app[i, j] <- nm_string[j]
  }
}
employers <- cbind(employers, emp_app)

可能不是最好的解決方案,但它可行

employee <- c('John','Peter','Jolie', 'Katy', 'Pauline')
numberofmonths <- c(7, 5, 11, 3, 12)
employers <- data.frame(employee,numberofmonths)
max = max(nchar(as.character(employers[,1])))
for (c in 1:max) {
    employers[,c+2] = ifelse(nchar(as.character(employers[,1]))>=c, paste0("A",c), NA)
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM