2016-10-22 81 views
-1

我想用正確的標題填充缺少的表格行,並在下面填充零。我也試圖用桌子進行劃分。用零填充缺失的列,然後將表格的底部行除以另一個表格的底部行

nba <- read.csv('nbadatasort.csv',header=FALSE) 

one <- grepl('\\Q+\\E',nba$V2) 
two <- grepl('\\Q*\\E',nba$V2) 
three <- grepl('\\Q^\\E',nba$V2) 
needed <- one | two | three 


allstar <- subset.data.frame(nba, needed) 

#This table lets me know how many people are in each draft number: It will return the following: 
# 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 
#25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 24 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 25 
#54 55 56 57 58 59 60 
#25 20 20 20 19 11 10 
table(nba$V1) 


#This table lets me know how many all stars each draft number had. It will return the following: 
#1 2 3 4 5 6 7 8 9 10 11 13 14 15 16 17 18 19 20 21 24 25 28 29 30 31 32 35 37 38 43 45 47 48 51 57 60 
#17 9 11 8 9 6 3 1 8 8 4 2 1 2 2 4 2 3 2 3 4 1 1 1 2 1 2 2 1 1 1 1 2 2 1 1 1 
table(allstar$V1) 

我的目標是在第二個表(ALLSTAR $ V1)和存在一個12其間的11與13和下12.一個零填充它以這樣然後我想將每個底部nba表中allstar表的值,這樣我得到值爲0.68爲1,0.36爲2等等。

任何幫助非常感謝。謝謝。

+0

而不是使用'「\\ \\ Q E」'轉義特殊字符的正則表達式,你也可以使用'固定= TRUE' 'grepl'中的參數將'pattern ='參數中的所有內容視爲固定字符串。 – useR

回答

0

如果我沒有誤解你的問題,你試圖找出每個草案號碼的allstars的百分比。這裏有一個方法,使用基礎R:

# Create test datasets 
set.seed(123) 
nba = sample(1:60, 500, replace = TRUE) 
allstar = sample(1:60, 100, replace = TRUE) 

# Count the occurences of each draft number and convert to dataframe 
nbadf = as.data.frame(table(nba)) 
allstardf = as.data.frame(table(allstar)) 

# Check the two dataframes 
unique(nbadf$nba) 
unique(allstardf$allstar) 

> unique(nbadf$nba) 
[1] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 
[28] 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 
[55] 55 56 57 58 59 60 
60 Levels: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 ... 60 
> unique(allstardf$allstar) 
[1] 1 2 3 4 5 6 8 10 11 13 17 18 19 20 21 22 23 24 25 26 27 28 30 31 32 33 34 
[28] 35 36 40 41 42 43 45 46 47 49 50 52 53 54 55 56 57 58 59 60 
47 Levels: 1 2 3 4 5 6 8 10 11 13 17 18 19 20 21 22 23 24 25 26 27 28 30 31 ... 60 

正如你所看到的,也有在NBA的數據幀60個的唯一號碼草案,但只有47個在全明星數據幀的唯一號碼草案(類似於你有什麼) 。現在merge這兩個數據幀使用nbadf中的「nba」和allstardf中的「allstar」作爲關鍵字。該all = TRUE意味着我們想要一個外連接,這正是我們想要的:

# Use merge to "Outer Join" the two dataframes 
fullDF = merge(nbadf, allstardf, by.x = "nba", by.y = "allstar", all = TRUE) 

# Replace the NA produced by the missing rows in allstardf with zeros 
fullDF[is.na(fullDF)] = 0 

# Calculate percentage of allstars for each draft number 
fullDF$Percent = with(fullDF, Freq.y/Freq.x) 

# Optionally rename the columns 
names(fullDF) = c("Draft_Num", "All", "AllStar", "AllStarPercent") 

> head(fullDF) 
    Draft_Num All AllStar AllStarPercent 
1   1 6  1  0.1666667 
2   2 4  3  0.7500000 
3   3 7  3  0.4285714 
4   4 8  2  0.2500000 
5   5 6  3  0.5000000 
6   6 8  2  0.2500000