2017-06-20 53 views
0

我有数据以这种形式如何多行转换成多列在一排中的R

  V1 V2 

1  6  1 

2  6  5 

3  1  0 

4  1  6 

5  1 385 

6  5  4 

7  5  6 

8  5 98 

9  0  1 

10  0  2 

,我想将其转换成

  V1 V2 V3 V4 

1  6  1  5 

2  1  0  6  385 

3  5  4  6  98 

4  0  1  2 

任何建议做成R

+0

如果你只对V1矢量的值感兴趣,也许'split'对你有用。否则,搜索“在R中重新整形”。 – lmo

+1

You van使用'tidyr :: nest(df,V2)'创建列表列 – HubertL

+0

新格式的逻辑是什么? – CPak

回答

0

请看下面,让我知道,如果这对你的作品:

# Data 
df <- data.frame(V1 = c(6,6,1,1,1,5,5,5,0,0), V2 = c(1,5,0,6,385,4,6,98,1,2)) 

# Splitting 
df.split <- split(df$V2, df$V1) 

# Combining 
maxLength <- max(rapply(df.split, length)) 
# initialize 
new <- list() 
z <- NULL # hold the object for length editing to include NAs 
for(i in 1:length(df.split)){ 
    z <- df.split[[i]] 
    length(z) <- maxLength 
    new[[i]] <- c(as.numeric(names(df.split))[i], z) 
} 
final <- as.data.frame(do.call(rbind,new)) 
[,1] [,2] [,3] [,4] 
[1,] 0 1 2 NA 
[2,] 1 0 6 385 
[3,] 5 4 6 98 
[4,] 6 1 5 NA 
0

这是一个dplyr/tidyr解决方案。

library(stringr) 
library(dplyr) 
library(tidyr) 

# Create test dataframe 
df <- data.frame(V1 = c(6,6,1,1,1,5,5,5,0,0), 
       V2 = c(1,5,0,6,385,4,6,98,1,2)) 

# Group data by V1 column, pasting all V2 values into one row 
df <- df %>% 
     group_by(V1) %>% 
     summarise(V2 = paste(V2, collapse = ",")) 

# Get the number of columns to separate data into 
cols <- max(str_count(df$V2, ",")) + 1 

# Set temporary column names 
cols <- paste0("col", c(1:cols)) 

# Split V2 column into multiple columns 
df <- df %>% 
     separate(V2, into = cols, sep = ",", fill = "right") 

# Rename columns 
colnames(df) <- paste0("V", c(1:ncol(df))) 

# Convert to integer 
df[] <- lapply(df, as.integer) 
+0

感谢马特它工作:) –

+0

感谢马特的帮助。我有另一个问题所有其他列是字符格式有没有什么办法让他们在整数格式 –

+0

'df [] < - lapply(DF,as.integer)' –