感谢您的帮助。整天都在为此工作,没有尽头。
我有一个csv文件,每行多行。想要扩展其余的列以容纳“多”行。
我尝试了
df <- data.frame(email = c('email1@email.com','email2@email.com','email3@email.com'),
ip = c('1 1 2 2 3','2 2 2','3 3 3'),
other = c('x','y','z'))
#looks likes this
email ip other
1 email1@email.com 1 1 2 2 3 x
2 email2@email.com 2 2 2 y
3 email3@email.com 3 3 3 z
> df_to_be
email ip other
1 email1@email.com 1 x
2 email1@email.com 1 x
3 email1@email.com 2 x
4 email1@email.com 2 x
5 email1@email.com 3 x
6 email2@email.com 2 y
7 email2@email.com 2 y
8 email2@email.com 2 y
9 email3@email.com 3 z
10 email3@email.com 3 z
11 email3@email.com 3 z
由于第一行的“多”行数量,Email1重复了5次。 由于第二行的“多”行数量,电子邮件2重复3次。 由于第三行的“多”行数量,Email3重复了3次。
类似于其他列
#function to recreate table based on new row count
repFunc <- function(df, multi_row_c){
cols_rep <- names(df[which(!names(df) %in% c(multi_row_c))]) #columns to repeat
vec_rep = str_count(df[,multi_row_c],coll(" "))+1 #vector of number of repeats per row for multi_row_c
r1 = 1:nrow(df) #row index to repeat
print('column names to repeat')
print(cols_rep)
print('number of repeats per row')
print(vec_rep)
print('row index to repeat')
print(r1)
for (i in 1:length(cols_rep)) {
print(df[,cols_rep[i]])
# o<-rep(df[r1,cols_rep[i]],vec_rep)
}
# return(o)
}
repFunc(df,'ip')