मेरे पास एक डेटा फ्रेम है जो इस तरह दिखता है

       Date    Score
A       1-1      5
A       1-2      6
A       1-3      2
A       1-4      9
A       1-5      4
B       1-1      3
B       1-2      6
C       1-1      7
C       1-2      2
C       1-3      5

मैं अपने कॉलम के नाम बदलना चाहता हूं ताकि वे तारीख दिखा सकें और उनका नाम t1, t2, t3 आदि रख सकें। नया डेटा फ्रेम इस तरह दिखना चाहिए:

       t1    t2    t3    t4    t5    
A      5      6     2     9     4
B      3      6
C      7      2     5
 

आंकड़े

dput(head(comp_temp,20))
structure(list(Name = c("A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", 
"A", "A"), `Date time` = c("03/01/2020", "03/02/2020", 
"03/03/2020", "03/04/2020", "03/05/2020", "03/06/2020", "03/07/2020", 
"03/08/2020", "03/09/2020", "03/10/2020", "03/11/2020", "03/12/2020", 
"03/13/2020", "03/14/2020", "03/15/2020", "03/16/2020", "03/17/2020", 
"03/18/2020", "03/19/2020", "03/20/2020"), Score = c(35.4, 
36.5, 34.7, 34.2, 32.1, 25.5, 27, 32, 34.9, 33.5, 29.4, 22.3, 
23.8, 29.9, 33.5, 33.4, 33.9, 33.7, 40.3, 37.1)), row.names = c(NA, 
-20L), class = c("tbl_df", "tbl", "data.frame"))
r
0
Rnovice 23 सितंबर 2020, 19:26

2 जवाब

सबसे बढ़िया उत्तर

इस dplyr और tidyr दृष्टिकोण को आजमाएं। हेडर के मानों को मैप करने के लिए आप Date वैरिएबल को अलग कर सकते हैं और फिर pivot_wider() का उपयोग कर सकते हैं। यहाँ कोड:

library(dplyr)
library(tidyr)
#Code
df %>% separate(Date,c('V1','V2')) %>%
  mutate(V2=paste0('t',V2)) %>%
  pivot_wider(names_from = V2,values_from=Score) %>% select(-V1)

आउटपुट:

# A tibble: 1 x 6
  Var      t1    t2    t3    t4    t5
  <chr> <int> <int> <int> <int> <int>
1 A         5     6     2     9     4

उपयोग किए गए कुछ डेटा:

#Data
df <- structure(list(Var = c("A", "A", "A", "A", "A"), Date = c("1-1", 
"1-2", "1-3", "1-4", "1-5"), Score = c(5L, 6L, 2L, 9L, 4L)), class = "data.frame", row.names = c(NA, 
-5L))

अपडेट करें: आपके द्वारा साझा किए गए डेटा के लिए यह तरीका आजमाएं। मेरे पास नए मूल्यों के साथ मिश्रित डेटा है ताकि आप देख सकें कि आउटपुट कैसे उत्पन्न होता है:

#Code 2
mdf2 <- mdf %>%
  #First mutate date to similar format
  mutate(`Date time`=as.Date(`Date time`,'%m/%d/%Y'),
         Date=paste0('T',format(`Date time`,'%d'))) %>% select(-`Date time`) %>%
  #Reshape
  pivot_wider(names_from = Date,values_from=Score)

आउटपुट:

# A tibble: 2 x 21
  Name    T01   T02   T03   T04   T05   T06   T07   T08   T09   T10   T11   T12   T13   T14
  <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 A      35.4  36.5  34.7  34.2  32.1  25.5    27    32  34.9  33.5  29.4  22.3  23.8  29.9
2 B      35.4  36.5  34.7  34.2  32.1  25.5    27    32  34.9  33.5  29.4  22.3  23.8  29.9
# ... with 6 more variables: T15 <dbl>, T16 <dbl>, T17 <dbl>, T18 <dbl>, T19 <dbl>, T20 <dbl>

उपयोग किया गया नया डेटा:

#Data
mdf <- structure(list(Name = c("A", "A", "A", "A", "A", "A", "A", "A", 
"A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "B", 
"B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", "B", 
"B", "B", "B", "B", "B", "B"), `Date time` = c("03/01/2020", 
"03/02/2020", "03/03/2020", "03/04/2020", "03/05/2020", "03/06/2020", 
"03/07/2020", "03/08/2020", "03/09/2020", "03/10/2020", "03/11/2020", 
"03/12/2020", "03/13/2020", "03/14/2020", "03/15/2020", "03/16/2020", 
"03/17/2020", "03/18/2020", "03/19/2020", "03/20/2020", "03/01/2020", 
"03/02/2020", "03/03/2020", "03/04/2020", "03/05/2020", "03/06/2020", 
"03/07/2020", "03/08/2020", "03/09/2020", "03/10/2020", "03/11/2020", 
"03/12/2020", "03/13/2020", "03/14/2020", "03/15/2020", "03/16/2020", 
"03/17/2020", "03/18/2020", "03/19/2020", "03/20/2020"), Score = c(35.4, 
36.5, 34.7, 34.2, 32.1, 25.5, 27, 32, 34.9, 33.5, 29.4, 22.3, 
23.8, 29.9, 33.5, 33.4, 33.9, 33.7, 40.3, 37.1, 35.4, 36.5, 34.7, 
34.2, 32.1, 25.5, 27, 32, 34.9, 33.5, 29.4, 22.3, 23.8, 29.9, 
33.5, 33.4, 33.9, 33.7, 40.3, 37.1)), row.names = c(NA, -40L), class = c("tbl_df", 
"tbl", "data.frame"))
0
Duck 23 सितंबर 2020, 20:45

newdataframe= data.frame(t=t(df$Score),row.names = 'A')

0
Phil 23 सितंबर 2020, 20:16