我的R代码
data <- read.csv('filename.csv')
typof(data)
[1] "list"
str(data)
'data frame' : 9 obs. of 10 variables
$Name: Factor w/9 levels "Name 1", "Name 2",....
$Column2: chr "","Text1","","Text2"
$Column3: chr "Text2","Text3","","Text1"
$Column4: chr "","","","Text1"
#and so on
要求:
我想要的只是$Column2
,$Column3
,$Column4
,......如果有非空值,请添加前缀(Here this is
)和后缀({ {1}})。
因此,考虑以上completed
,目前有价值data Column2
的第2行应该变为"Text1"
同样在"Here this is Text1 completed."
中需要添加前缀和后缀值。
我不想使用循环,除非并且直到需要/必要。
我的尝试:
我尝试了一些尝试,例如Column3: 1st, 2nd and 4th cell
,interaction
,mget
等等,但似乎没有任何效果。
答案 0 :(得分:5)
我会按照以下方式对其进行矢量化
indx <- which(data[, -1] != "", arr.ind = TRUE) # Find all non-empty incidences
data[, -1][indx] <- paste("Here this is", data[, -1][indx], "completed.")
答案 1 :(得分:3)
这适用于前4列
apply(data[,2:4],2,function(x) ifelse(x != "",paste("Here this is ",x," completed."),x))
假设每列的前缀和后缀相同。它确实返回一个矩阵,但很容易将其转换为数据帧。 希望它有所帮助。
修改:刚刚意识到您的数据位于列表中,因此您需要lapply
或sapply
。类似的东西:
sapply(data,function(x) ifelse(x != "",paste("Here this is ",x," completed."),x))[,2:4]
也会返回一个矩阵。
答案 2 :(得分:1)
以下是set
的选项,可以在没有任何复制的情况下进行分配
library(data.table)
setDT(data)
for(j in 2:ncol(data)){
set(data, i = which(data[[j]]!=""),
j = j,
value = paste("Here there is ", data[[j]][data[[j]]!=""], " completed."))
}
data
# Name Column2 Column3
#1: Name 1 Here there is Text1 completed.
#2: Name 2 Here there is Text1 completed.
#3: Name 3 Here there is Text2 completed. Here there is Text2 completed.
#4: Name 4 Here there is Text3 completed.
data <- structure(list(Name = structure(1:4, .Label = c("Name 1", "Name 2",
"Name 3", "Name 4"), class = "factor"), Column2 = c("", "Text1",
"Text2", ""), Column3 = c("Text1", "", "Text2", "Text3")), .Names = c("Name",
"Column2", "Column3"), row.names = c(NA, -4L), class = "data.frame")
答案 3 :(得分:0)
使用# dummy data
df1 <- mtcars[1:5, 1:3]
# add blanks
df1[2,2] <- ""
df1
# mpg cyl disp
# Mazda RX4 21.0 6 160
# Mazda RX4 Wag 21.0 160
# Datsun 710 22.8 4 108
# Hornet 4 Drive 21.4 6 258
# Hornet Sportabout 18.7 8 360
# add prefix and suffix
res <- cbind(df1[, 1, drop = FALSE],
data.frame(
lapply(df1[, -1], function(i)
ifelse(i == "", i, paste("Here this is", i, "completed.")))))
res
# mpg cyl disp
# Mazda RX4 21.0 Here this is 6 completed. Here this is 160 completed.
# Mazda RX4 Wag 21.0 Here this is 160 completed.
# Datsun 710 22.8 Here this is 4 completed. Here this is 108 completed.
# Hornet 4 Drive 21.4 Here this is 6 completed. Here this is 258 completed.
# Hornet Sportabout 18.7 Here this is 8 completed. Here this is 360 completed.
函数(也是循环):
func setChart(dataPoints: [String], values: [Double]) {
radarChartView.noDataText = "You need to provide data for the chart."
var dataEntries: [ChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = ChartDataEntry(value: values[i], xIndex: i)
dataEntries.append(dataEntry)
}
let chartDataSet = RadarChartDataSet(yVals: dataEntries, label: "Units Sold")
//Set options...
//Then set data
let chartData = RadarChartData(xVals: subjects, dataSet: chartDataSet)
}
答案 4 :(得分:-1)
DF = transform(ifelse(data$Column2 == "", data$Column2, sprintf('Here it is %s completed', data$Column2)))
DF <- data.frame (DF, data$Name, data$Column3)