简体   繁体   English

如何以不同格式从R的CSV文件中读取一列值

[英]How to read one column value from CSV file in R in distinct format

How to use legend in R for the following data 如何在R中将图例用于以下数据

ID  Gender    Age   Site      Times
1   Male      24    Facebook     10
2   Female    24    Linkedin     10
3   Male      24    Twitter      10
4   Female    24    Myspace      10
5   Male      24    Facebook     10
6   Female    24    linkedin     10
7   Male      24    Facebook     10

TO Read the CSV I've used 读取我使用过的CSV

pd.readcsv <- read.csv(file = "snsite.csv")

To design the Pie chart I've used 设计我使用的饼图

pie(pd.freq[order(pd.freq,decreasing = T)],
col = c("Blue","Green","Yellow","Brown","Green"),
border = NA, main = "Site Usage")

Now to show legend at top right or top left, how to read one column(Site) value in distinct form from csv file and store it in a variable and used it in legend function? 现在要在右上角或左上角显示图例,如何从csv文件中以不同的形式读取一列(Site)值并将其存储在变量中并在图例函数中使用?

I wasn't sure where you were getting the pd.freq object from, but assuming you are trying to get a pie chart showing per site how many times each site was visited, then this should do the trick. 我不确定从哪里获得pd.freq对象,但是假设您试图获取一个饼图,该饼图显示每个站点访问了每个站点的次数,那么就可以解决问题。

# Read dataframe, I would recommend including the stringsAsFactors = FALSE argument to your read.csv command
pd.readcsv <- read.table(text = "ID  Gender    Age   Site      Times
                                  1   Male      24    Facebook     10
                                  2   Female    24    Linkedin     10
                                  3   Male      24    Twitter      10
                                  4   Female    24    Myspace      10
                                  5   Male      24    Facebook     10
                                  6   Female    24    linkedin     10
                                  7   Male      24    Facebook     10", 
                                  header = TRUE, stringsAsFactors = FALSE)

# Convert sites to lowercase for proper aggregation
pd.readcsv$Site <- sapply(pd.readcsv$Site, tolower)

# Get vector of unique site names
sites <- unique(pd.readcsv$Site)

# Create aggregate dataframe with total times per site
pd.SumSiteTimes <- aggregate(Times~Site, sum, data = pd.readcsv)

# Create vector for colors
colors <- c("Blue","Green","Yellow","Brown","Green")

# Create pie chart
pie(pd.SumSiteTimes$Times[order(pd.SumSiteTimes$Times,decreasing = T)],
    col = colors,
    border = NA, 
    main = "Site Usage")

# Add legend to the plot
legend("topright", sites, cex = .8, fill = colors)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从R中的.csv文件中读取列向量 - How to read in column vectors from a .csv file in R 如何从R中的csv文件读取一列时间以进行数学运算? - How to read a column of time from a csv file in R for mathematical operations? R:如果另一列具有不同的值,如何使用aggregate()函数对某一列的数据求和? - R: how to use the aggregate()-function to sum data from one column if another column has a distinct value? How to use R shiny to filter a specific column from a csv file and extract the data in csv and pdf format - How to use R shiny to filter a specific column from a csv file and extract the data in csv and pdf format R中使用read.csv读取的文件中的字符列出现问题 - Trouble with character column from a file read in with read.csv in r 阅读csv的第四列,并在r中合并为一个文件 - Read Fourth column of csv and combine into one file in r 从 lapply 的 R 中的 csv 文件中的单元格读取值 - Read the value from a cell in a csv file in R from lapply 如何在R闪亮中读取导入的csv文件中的列 - How to read a column in a imported csv file in R shiny 我如何获取R中的列A值等于列B值的数据帧的子集(从CSV读取的数据) - How do i get subset of a data frame where Column A value equals Column B value in R ( Data read from CSV) 如何在R中读取一种以上语言的.csv文件? - How to read a .csv file with more than one language in R?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM